<?php
$basket = [
[
'name' => 'apple',
'qty' => 5,
],
[
'name' => 'apple',
'qty' => 4,
],
[
'name' => 'pear',
'qty' => 4,
]
];
// need a function that would convert this to
$output = [];
foreach ($basket as $item) {
$key = $item['name'];
if (!isset($output[$key])) $output[$key] = 0;
$output[$key] += $item['qty'];
}
var_dump($output);
//$output = [['name' => 'apple', 'qty' => 9], ['name' => 'pear', 'qty' => 4]];
preferences:
30.84 ms | 404 KiB | 5 Q