PHP的阵列组 [英] php array group
本文介绍了PHP的阵列组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下阵列
Array
(
[0] => Array
(
[id] => 96
[shipping_no] => 212755-1
[part_no] => reterty
[description] => tyrfyt
[packaging_type] => PC
)
[1] => Array
(
[id] => 96
[shipping_no] => 212755-1
[part_no] => dftgtryh
[description] => dfhgfyh
[packaging_type] => PC
)
[2] => Array
(
[id] => 97
[shipping_no] => 212755-2
[part_no] => ZeoDark
[description] => s%c%s%c%s
[packaging_type] => PC
)
)
我怎么能组阵列由 ID
?是否有任何PHP函数都可以做到这一点?
How can I group the array by id
? Is there any native php functions are available to do this?
如果我的foreach
上面的话,我会得到一个重复的,我怎么能避免呢?
If I foreach
the above then I will get a duplicate one, how can I avoid that?
在上面的例子中 ID
有2项,所以它需要在 ID
里面。
On the above example id
have 2 items, so its need to be inside of the id
.
编辑:一切正常:但有没有达到同样的一个FOREACH任何方式
推荐答案
有没有原生的,只需使用一个循环。
There is no native one, just use a loop.
$result = array();
foreach ($arr as $data) {
$id = $data['id'];
if (isset($result[$id])) {
$result[$id][] = $data;
} else {
$result[$id] = array($data);
}
}
这篇关于PHP的阵列组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文