Maybe someone has already asked, but I didn’t find the right answer. I need group arrays by mpn
and product_id
key values and count it’s quantities.
my array:
[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
)
the desired result:
[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
)
I have tried this suggestion Group array values based on key in php? but no success.
2
Answers
With this code you can obtain an array group by mpn. With $myList is your original array.
Here’s a working example using a simple loop.