如何解析分隔的字符串并合并到最终的数组中

时间:2013-10-17 22:44:26

标签: php

我在使用此功能时遇到问题。这是一个客户端自动操作盒。客户可以拥有3个不同的联系人及其各自的电话号码,以及他们的地址等。

从数据库中检索到这些数据后,我需要将其合并到客户端数组中,以便可以向用户显示。

这就是现在来自数据库的数据以及其他客户数据:

Array
(
  [0] => Array
    (
        [ClientName] => Test 1
        [ClientAddress] => 1234 Maple St
        [Contacts] => Sue Miller:7495872472:1,Paul Miller:8975247624:2,Amy Miller:9762547627:3
        [ClientId] => 22
    )
)

我需要将联系人分开,解析字符串并最终得到一个如下所示的数组:

Array
(
  [0] => Array
    (
        [ClientName] => Test 1
        [ClientAddress] => 1234 Maple St
        [name1] => Sue Miller
        [phone1] => 7495872472
        [contact1Id] => 1
        [name2] => Paul Miller
        [phone2] => 8975247624
        [contact2Id] => 2
        [name3] => Amy Miller
        [phone3] => 9762547627
        [contact3Id] => 3
        [ClientId] => 22
    )
)

我怎样才能做到这一点?有人可以在这里指导我吗?

3 个答案:

答案 0 :(得分:0)

这是非常简单的解析:

<?php

$data = "Sue Miller:7495872472:1,Paul Miller:8975247624:2,Amy Miller:9762547627:3";

$contacts = explode(',', $data);

foreach ($contacts as $number => $contact) {
    $info = explode(':', $contact);
    $contactInfo["name{$number}"] = $info[0];
    $contactInfo["phone{$number}"] = $info[1];
    $contactInfo["contact{$number}Id"] = $info[2];
}

print_r($contactInfo);

答案 1 :(得分:0)

您需要使用几个explode来将您的联系人字符串分解为第一个联系人,然后是姓名,电话和联系人ID。然后你将它们与原始数据放在数组中:

$new_array = array();

foreach($array as $key => $current) {
    $contacts = explode(",", $current['Contacts']);
    $new_array[$key] = array(
        'ClientName' => $current['ClientName'],
        'ClientAddress' => $current['ClientAddress']
    );
    $i = 1;
    foreach($contacts as $contact) {
        list($name, $phone, $clientID) = explode(":", $contact);
        $new_array[$key]['name' . $i] = $name;
        $new_array[$key]['phone' . $i] = $phone;
        $new_array[$key]['contact' . $i . 'Id'] = $clientID;
        $i++;
    }
    $new_array[$key]['ClientId'] = $current['ClientId'];
}

echo '<pre>'; print_r($new_array); exit;

结果:

Array
(
    [0] => Array
        (
            [ClientName] => Test 1
            [ClientAddress] => 1234 Maple St
            [name1] => Sue Miller
            [phone1] => 7495872472
            [contact1Id] => 1
            [name2] => Paul Miller
            [phone2] => 8975247624
            [contact2Id] => 2
            [name3] => Amy Miller
            [phone3] => 9762547627
            [contact3Id] => 3
            [ClientId] => 22
        )

)

答案 2 :(得分:0)

只是为了分享答案。更新Klaus答案并添加我的答案,我假设您将处理大量数据。

$myarr = array(
    array('ClientName' => 'Test 1',
        'ClientAddress' => '1234 Maple St',
        'Contacts' => 'Sue Miller:7495872472:1,Paul Miller:8975247624:2,Amy Miller:9762547627:3',
        'ClientId' => '22')
);



foreach($myarr as $index => $arr){

    foreach($arr as $key => $value){

        if($key == 'Contacts'){ //check if key is Contacts before processing
         $user_contacts = explode(',', $value);

            foreach ($user_contacts as $number => $contact) {
                $number++; //increment the index to start at 1 and not with 0

                $info = explode(':', $contact);
                $contactInfo["name{$number}"] = $info[0];
                $contactInfo["phone{$number}"] = $info[1];
                $contactInfo["contact{$number}Id"] = $info[2];
            }


            $newArr = array_merge((array)$arr, (array)$contactInfo);
        }
    }
}

print_r($newArr);