3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = Array ( 0 => Array ( "year" => "2016", "numb" => 1 ), 1 => Array ( "year" => "2016", "numb" => 3 ), 2 => Array ( "year" => "2017", "numb" => 3 ), 3 => Array ( "year" => "2016", "numb" => 1 ), 4 => Array ( "year" => "2018", "numb" => 2 ) ); // create new array with year as key and numb as value $res =array(); Foreach($arr as $val){ If(!isset($res[$val["year"]])) $res[$val["year"]] =0; $res[$val["year"]] += $val["numb"]; } ksort($res); // sort the array on keys to make earliest year first $newarr= array(); $sum =0; foreach($res as $key => $val){ $newarr[$key] = $val+$sum; // new array with summed values from previous year(s) $sum +=$val; } Var_dump($newarr);
Output for git.master, git.master_jit, rfc.property-hooks
array(3) { [2016]=> int(5) [2017]=> int(8) [2018]=> int(10) }

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:
122.43 ms | 405 KiB | 5 Q