3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php // Everything you enter here will be executed by our servers. Try it! $year = array(2011, 2011, 2011, 2012, 2012, 2013, 2013, 2013); $profit = array(23.42, 253.23, 54.5, 433.23, 53.2, 11.4, 45.5, 532.1); $sums = array(); for ($i = 0; $i < count($year); $i++) { if (isset($sums[$year[$i]])) { $sums[$year[$i]] += $profit[$i]; } else { $sums[$year[$i]] = $profit[$i]; } } print_r($sums);
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Array ( [2011] => 331.15 [2012] => 486.43 [2013] => 589 )