This is a section of my array:
[1] => Array
(
[quantity] => 2
[product_id] => 1
[option_id] => 22
)
[2] => Array
(
[quantity] => 2
[product_id] => 2
[option_id] => 22
)
[3] => Array
(
[quantity] => 3
[product_id] => 2
[option_id] => 22
)
[4] => Array
(
[quantity] => 1
[product_id] => 2
[option_id] => 25
)
I wish to group/merge the subarrays by product_id
and option_id
.
Upon merging subarrays, I would like to sum the quantity
values.
In my sample data, both subarrays [2]
and [3]
have 'product_id'=>2
and 'option_id'=>22
. They should be merged together into one subarray with a quantity
value of 5
.
This is my expected output:
[1] => Array
(
[quantity] => 2
[product_id] => 1
[option_id] => 22
)
[2] => Array
(
[quantity] => 5
[product_id] => 2
[option_id] => 22
)
[3] => Array
(
[quantity] => 1
[product_id] => 2
[option_id] => 25
)
*My first level keys are not associated with their subarrays so they may be changed in the process. I do want the first level keys to be incremented from 1
not 0
.