也许有人已经问过了,但我没有找到正确的答案。我需要按mpn
和product_id
键值对数组进行分组,并计算其数量。我的数组:
[0] => Array
(
[product] => Product HTC
[mpn] =>
[quantity] => 3
[product_id] => 28
)
[1] => Array
(
[product] => Product HTC
[mpn] => ggg
[quantity] => 5
[product_id] => 28
)
[2] => Array
(
[product] => Product HTC
[mpn] => ggg
[quantity] => 1
[product_id] => 28
)
[3] => Array
(
[product] => Product HTC
[mpn] => ggg
[quantity] => 1
[product_id] => 28
)
[4] => Array
(
[product] => Product HTC
[mpn] => fff
[quantity] => 1
[product_id] => 28
)
期望的结果:
[0] => Array
(
[product] => Product HTC
[mpn] =>
[quantity] => 3
[product_id] => 28
)
[1] => Array
(
[product] => Product HTC
[mpn] => ggg
[quantity] => 7
[product_id] => 28
)
[2] => Array
(
[product] => Product HTC
[mpn] => fff
[quantity] => 1
[product_id] => 28
)
我尝试过这个建议Group array values based on key in php?,但没有成功。
2条答案
按热度按时间4jb9z9bj1#
通过这段代码,你可以得到一个按mpn分组的数组。其中**$myList**是你的原始数组。
8yparm6h2#
下面是一个使用简单循环的工作示例。