Php - Sum up the numbers in an array one by one



PHP Snippet 1:

$arr=array(1100,3150,4430,4430,5170,7450,7450,7450,8230);
$out = array($arr[0]);
for ($i = 1; $i < count($arr); $i++) {
    $out[$i] = $out[$i-1] + $arr[$i];
}
$arr = array_combine($out, $arr);
print_r($arr);

PHP Snippet 2:

Array (
  [1100] => 1100
  [4250] => 3150
  [8680] => 4430
  [13110] => 4430
  [18280] => 5170
  [25730] => 7450
  [33180] => 7450
  [40630] => 7450
  [48860] => 8230
)

PHP Snippet 3:

$x = array();
$y = array();
$running_total = 0;
for($i = 0; $i < count($arr); $i++){
    $y[$i] = $arr[$i];
    $running_total += $arr[$i];
    $x[$i] = $running_total;
}

PHP Snippet 4:

$arr = [1100,3150,4430,4430,5170,7450,7450,7450,8230];

$result = [];
$previous = 0;
foreach ($arr as $value) {
    $result[$previous += $value] = $value;
}
var_export($result);
// same result as @Nick's answer