如何按列值对子数组进行分组? [英] How to group subarrays by a column value?
本文介绍了如何按列值对子数组进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数组
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
.
所有工作都很好:但有什么方法可以一次实现相同的目标吗?
推荐答案
没有原生的,只需使用循环即可.
There is no native one, just use a loop.
$result = array();
foreach ($data as $element) {
$result[$element['id']][] = $element;
}
这篇关于如何按列值对子数组进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文