3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '[{"clientName":"Audi","model":"A6","cost":40000},{"clientName":"Audi","model":"S8","cost":90000},{"clientName":"VW","model":"GolfV","cost":5000},{"clientName":"VW","model":"GolfV","cost":3500},{"clientName":"Audi","model":"RS6","cost":120000},{"clientName":"VW","model":"PassatCC","cost":35000}]'; $array = json_decode($json, true); $totals = []; $makes = []; $modelTotals = []; foreach ($array as $row) { // Set defaults to suppress notices $totals[$row['clientName']] = isset($totals[$row['clientName']]) ? $totals[$row['clientName']] : 0; $modelTotals[$row['model']] = isset($modelTotals[$row['model']]) ? $modelTotals[$row['model']] : 0; $totals[$row['clientName']] += $row['cost']; $makes[$row['clientName']][$row['model']] = $row; $modelTotals[$row['model']] += $row['cost']; } foreach($makes as $client => $cars) { echo $client.' total: '.$totals[$client]."\n"; foreach($cars as $model) { echo $model['model'].' '.$modelTotals[$model['model']]."\n"; } }
Output for git.master, git.master_jit, rfc.property-hooks
Audi total: 250000 A6 40000 S8 90000 RS6 120000 VW total: 43500 GolfV 8500 PassatCC 35000

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
25.85 ms | 405 KiB | 5 Q