2

我有一个问题,让这个功能恰到好处。这是一个客户端自动竞争框。客户可以有 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
    )
)

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

4

3 回答 3

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);
于 2013-10-17T22:55:18.903 回答
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
        )

)
于 2013-10-17T23:03:41.767 回答
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);
于 2013-10-17T23:26:00.963 回答