The issue lies not in the counting itself, but rather in setting the key-value pairs. Here's the snippet of code:
<?php
$jsonPacks = '{
"pack": [
{
"container": 50,
"layout": [
{
"id": 4054,
"stacksize": 1
}
]
},
...
]
}';
function _countItemsByContainer($containerId, $arrayPack) {
$obj = [];
...
}
$arrayPack = json_decode($jsonPacks, true);
$totalCartonizations = count($arrayPack['pack']);
$countItemsByContainer = [];
for ($k = 0;$k < $totalCartonizations;$k++){
$countItemsByContainer[] = _countItemsByContainer($k, $arrayPack);
}
print_r(json_encode($countItemsByContainer));
?>
This is the output from the code:
[
{
"4054": 1
},
...
]
Which is accurate. The count reflects the number of each id in the 6 packs.
The desired JSON structure is as follows:
{
"pack": [
{
"item": [
{
"id": 4054,
"total": 1
},
]
},
...
]
}
Your assistance would be highly appreciated.
Thank you.