我有以下数组
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
对数组进行分组?是否有任何 native php 函数可用于执行此操作?
虽然这种方法有效,但我想使用 foreach
来执行此操作,因为使用上面的方法我会得到重复的项目,这是我试图避免的?
在上面的例子中,id
有2个项目,所以它需要在id
最佳答案
没有原生的,用循环就好了。
$result = array();
foreach ($data as $element) {
$result[$element['id']][] = $element;
}
关于php - 如何按列值对子数组进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12706359/