I wanna know how to iterate an array to count elements in PHP, I've trying next,
foreach ($items as $item) {
$tm[$item->provider->name] = [];
foreach ($items as $item) {
$tm[$item->provider->name][$item->product->brand->name] = isset($tm[$item->provider->name][$item->product->brand->name]) ? $tm[$item->provider->name][$item->product->brand->name] + 1 : $tm[$item->provider->name][$item->product->brand->name] = 1;
}
}
But I get a wrong result, I get an array but I get a very high number count as if iterated many timesmany times
The structure of the array is as follows
[{
"id": 1,
"product": {
"id": 1,
"brand": {
"id": 1,
"name": "iphone"
}
},
"provider": {
"id": 1,
"name": "at&t"
}
},
{
"id": 2,
"product": {
"id": 2,
"brand": {
"id": 2,
"name": "iphone"
}
},
"provider": {
"id": 1,
"name": "at&t"
}
},
{
"id": 3,
"product": {
"id": 3,
"brand": {
"id": 3,
"name": "iphone"
}
},
"provider": {
"id": 1,
"name": "t-mobile"
}
}]