I\'m trying to create an array that groups together similar/identical items and creates a count/ tally of the number of times that item occurs.
I\'ve managed to get an a
The outer part of your loop looks like it should work for this. You just need to make a couple of changes to how it is building the grouped array.
foreach ($orders as $order) {
foreach ($order['lineItems']['elements'] as $item) {
// use the item id as a key in the array you are building
$id = $item['item']['id'];
if (isset($products[$id])) {
// if the item has already been added, increment the count
$products[$id]['Count']++;
} else {
// otherwise, add the item with an initial count of 1
$products[$id]['ID'] = $id;
$products[$id]['Item'] = $item['name'];
$products[$id]['Count'] = 1;
}
}
}
The resulting $products array will have item IDs as keys. If you want it to have numeric keys instead, you can then do:
$products = array_values($products);