3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $originArr=array( array('amount'=>100,'date'=>'2014-03-29'), array('amount'=>120,'date'=>'2014-03-30'), array('amount'=>200,'date'=>'2014-03-31'), array('amount'=>300,'date'=>'2014-04-31'), array('amount'=>400,'date'=>'2014-05-31') ); $newArr1 = array(); $newArr2 = array(); $tempArr = array(); $resultArr = array(); foreach($originArr as $el) { $newArr1[$el['date']] = $el['amount']; } foreach ($newArr1 as $k1 => $v1) { foreach ($newArr1 as $k2 => $v2) { $v1 += ($k1 > $k2) ? $v2 : 0; } $newArr2[$k1] = $v1; } foreach($newArr2 as $key => $value) { $tempArr['amount'] = $value; $tempArr['date'] = $key; $resultArr[] = $tempArr; } var_dump($resultArr);
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
array(5) { [0]=> array(2) { ["amount"]=> int(100) ["date"]=> string(10) "2014-03-29" } [1]=> array(2) { ["amount"]=> int(220) ["date"]=> string(10) "2014-03-30" } [2]=> array(2) { ["amount"]=> int(420) ["date"]=> string(10) "2014-03-31" } [3]=> array(2) { ["amount"]=> int(720) ["date"]=> string(10) "2014-04-31" } [4]=> array(2) { ["amount"]=> int(1120) ["date"]=> string(10) "2014-05-31" } }