合并多维数组的数据

发布于 2024-11-29 10:13:49 字数 1334 浏览 0 评论 0原文

我有这种数组

Array ( [0] => Array (  [id] => 1 
                        [name] => PeopleOne
                        [address] => AddressOneOfPeopleOne 

        [1] => Array (  [id] => 2 
                        [name] => PeopleTwo 
                        [address] => AddressOneOfPeopleTwo 

        [2] => Array (  [id] => 3 
                        [name] => PeopleThree 
                        [address] => AddressOneOfPeopleThree 

        [3] => Array (  [id] => 4 
                        [name] => PeopleOne
                        [address] => AddressTwoOfPeopleOne 

,我想要这种格式,

Array ( [0] => Array (  [id] => 1 
                        [name] => PeopleOne
                        [address] => Array(
                                    [0] => AddressOneOfPeopleOne
                                    [1] => AddressTwoOfPeopleOne
                        )

        [1] => Array (  [id] => 2 
                        [name] => PeopleTwo 
                        [address] => AddressOneOfPeopleTwo 

        [2] => Array (  [id] => 3 
                        [name] => PeopleThree 
                        [address] => AddressOneOfPeopleThree 

我不知道该怎么办? 有人可以解决这个问题吗?

I have this kind of array

Array ( [0] => Array (  [id] => 1 
                        [name] => PeopleOne
                        [address] => AddressOneOfPeopleOne 

        [1] => Array (  [id] => 2 
                        [name] => PeopleTwo 
                        [address] => AddressOneOfPeopleTwo 

        [2] => Array (  [id] => 3 
                        [name] => PeopleThree 
                        [address] => AddressOneOfPeopleThree 

        [3] => Array (  [id] => 4 
                        [name] => PeopleOne
                        [address] => AddressTwoOfPeopleOne 

and I want this kind of format

Array ( [0] => Array (  [id] => 1 
                        [name] => PeopleOne
                        [address] => Array(
                                    [0] => AddressOneOfPeopleOne
                                    [1] => AddressTwoOfPeopleOne
                        )

        [1] => Array (  [id] => 2 
                        [name] => PeopleTwo 
                        [address] => AddressOneOfPeopleTwo 

        [2] => Array (  [id] => 3 
                        [name] => PeopleThree 
                        [address] => AddressOneOfPeopleThree 

I don't know how to do?
Could anyone please solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑叹一世浮沉 2024-12-06 10:13:49

我有一种感觉,这并不完全是您正在寻找的,但我不完全确定最终数组中需要什么。至少在一开始,情况如何?

<?php
$array = array(
array('id' => 1, 'name' => 'PeopleOne', 'address' => 'Address1'),
array('id' => 2, 'name' => 'PeopleTwo', 'address' => 'Address2'),
array('id' => 3, 'name' => 'PeopleOne', 'address' => 'Address3')
);


foreach ($array as $k => $v) {
$newarray[$v['name']][] = $v['address'];
}

echo '<pre>'.print_r($array,1).'</pre>';
echo '<pre>'.print_r($newarray,1).'</pre>';

?>

I have a feeling this isn't exactly what you're looking for, but I'm not entirely sure what needs to be inside the final array. How is this, at least for a start?

<?php
$array = array(
array('id' => 1, 'name' => 'PeopleOne', 'address' => 'Address1'),
array('id' => 2, 'name' => 'PeopleTwo', 'address' => 'Address2'),
array('id' => 3, 'name' => 'PeopleOne', 'address' => 'Address3')
);


foreach ($array as $k => $v) {
$newarray[$v['name']][] = $v['address'];
}

echo '<pre>'.print_r($array,1).'</pre>';
echo '<pre>'.print_r($newarray,1).'</pre>';

?>
攒眉千度 2024-12-06 10:13:49

我假设你的数组名为 $array。

foreach ($array as $subArray) {
    if (!is_array($subArray['address']) {
        $subArray['address'] = array($subArray['adress']);
    }
    $subArray['address'][] = 'AddressTwoOfPeopleOne';
}

I'll assume that your array is called $array.

foreach ($array as $subArray) {
    if (!is_array($subArray['address']) {
        $subArray['address'] = array($subArray['adress']);
    }
    $subArray['address'][] = 'AddressTwoOfPeopleOne';
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文