3v4l.org

run code in 300+ PHP versions simultaneously
<?php $j = <<<EOD [{"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"}}] EOD; $items = json_decode($j); $tm = array(); foreach ($items as $item) { $product = $item->product->brand->name; $provider = $item->provider->name; $tm[$provider][$product] = ($tm[$provider][$product] ?? 0) + 1; } print_r($tm);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
Array ( [at&t] => Array ( [iphone] => 2 ) [t-mobile] => Array ( [iphone] => 1 ) )

preferences:
99.54 ms | 1262 KiB | 4 Q