gpt4 book ai didi

php - 如何按列值对子数组进行分组?

转载 作者:IT老高 更新时间:2023-10-28 12:05:45 32 4
gpt4 key购买 nike

我有以下数组

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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com