I have a dynamic array, it contains keys that duplicate (TAG):
[0] => Array
(
[0] => Array
(
[tag] => 5x1 Acessori Kids
[pedido] => 6701622409
[quantidade] => 2125
)
)
[1] => Array
(
[1] => Array
(
[tag] => 5x1 Acessori Kids
[pedido] => 6701622422
[quantidade] => 3705
)
)
[2] => Array
(
[2] => Array
(
[tag] => RIABASIC5- 5X1 - 2 COMPOSIÇOES
[pedido] => 6701622411
[quantidade] => 3165
)
)
I would like to add the "quantity" keys and group the "request" key with a comma, output would look like this:
[0] => Array
(
[0] => Array
(
[tag] => 5x1 Acessori Kids
[pedido] => 6701622422, 6701622409
[quantidade] => 5830
)
)
[1] => Array
(
[1] => Array
(
[tag] => RIABASIC5- 5X1 - 2 COMPOSIÇOES
[pedido] => 6701622411
[quantidade] => 3165
)
)
The sum in the 'quantity' key I got with the following function:
$sum = array_reduce($data, function ($a, $b) {
isset($a[$b['tag']]) ? $a[$b['tag']]['quantidade'] += $b['quantidade'] : $a[$b['tag']] = $b;
return $a;
});
$data = array_values($sum);
but I'm not able to concatenate the 'request' key, which would be for this example above:
[pedido] => 6701622422, 6701622409
Does anyone have any tips?