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(2010,2010,2010,2011,2011,2012,2013); $profit = array(100,50,10,80,70,50,40); $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 - 7.1.0
Array ( [2010] => 160 [2011] => 150 [2012] => 50 [2013] => 40 )