0
I have a dynamic array, it contains duplicate keys (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 add the keys "quantity" and group the key "request" with a comma, output would be 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
)
)
sum in the quantity key achieved 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);
however I am not able to concatenate the key 'request', which would be for this example above:
[pedido] => 6701622422, 6701622409
anyone has any tips?
Already considered updating PHP?
– Woss