Sum each last value with all previous values ​​in the array

I have an array of some values ​​that I need to convert to a new array and sum each value with all previous values. For example (array length, keys and values ​​are always different), this is what I have:

Array ( [0] => 1 [1] => 1 [2] => 5 [3] => 1 [4] => 1 [7] => 1 [8] => 3 [9] => 1 ) 

and this is what i need:

 Array ( [0] => 1 [1] => 2 [2] => 7 [3] => 8 [4] => 9 [7] => 10 [8] => 13 [9] => 14 ) 

I tried many different ways, but I was always stuck in something or realized that I was wrong somewhere. I feel like I'm trying to invent a wheel here because I think it needs some kind of simple function, but he couldn’t find a solution. This is the last way I tried:

 $array = array( "0"=> 1, "1"=> 1, "2"=> 5, "3"=> 1, "4"=> 1, "7"=> 1, "8"=> 3, "9"=> 1 ); $this = current($array); $next = next($array); $end = next(end($array)); $sum = 0; $newArray = array(); foreach ($array as $val){ if($val != $end){ $sum = ($this += $next); array_push($newArray, $sum); } } print_r($newArray); 

.. Sorry, wrong again. I spend a lot of time looking for ways to avoid getting to where I should be, can someone hit me in the right direction, please?

+6
source share
5 answers

Suggest using array_slice() and array_sum()

 $array = array( "0"=>1,"1"=>1,"2"=>5,"3"=>1,"4"=>1,"7"=>1,"8"=>3,"9"=>1); $keys = array_keys($array); $array = array_values($array); $newArr = array(); foreach ($array as $key=>$val) { $newArr[] = array_sum(array_slice($array, 0, $key+1)); } $newArr = array_combine($keys, $newArr); print '<pre>'; print_r($newArr); print '</pre>'; 

Output:

 Array ( [0] => 1 [1] => 2 [2] => 7 [3] => 8 [4] => 9 [7] => 10 [8] => 13 [9] => 14 ) 

Link:

+4
source

let's say your array variable is $ a; You can use a simple loop

  for($i=1;$i<sizeof($a);$i++) { $a[$i]=$a[$i]+$a[$i-1]; } 
+4
source
 $sums = array_reduce($array, function (array $sums, $num) { return array_merge($sums, [end($sums) + $num]); }, []); 

In other words, it is iterating over the array ( array_reduce ) and at each iteration adds ( array_merge ) a number to the new array ( $sums ), which is the sum of the previous element in the array ( end($sums) ) and the current number. Uses PHP 5.4+ array syntax ( [] ).

+2
source

You can simply list the sum of the array and add to the new array (in a simple way):

 $array = array( "0"=> 1, "1"=> 1, "2"=> 5, "3"=> 1, "4"=> 1, "7"=> 1, "8"=> 3, "9"=> 1 ); var_dump($array); $newArray = array(); $sum = 0; foreach ($array as $a){ $sum += $a; $newArray[]=$sum; } var_dump($newArray); 
+1
source

Too complicated. Scroll through all the elements of the array and add the current element to the sum. Then assign this amount to the new array.

 $array = array( "0"=> 1, "1"=> 1, "2"=> 5, "3"=> 1, "4"=> 1, "7"=> 1, "8"=> 3, "9"=> 1 ); $sum = 0; $newArray = array(); foreach ($array as $key=>$val){ $sum += $val; $newArray[$key]=$sum; } 

Make sure the indexes are correct.

+1
source

Source: https://habr.com/ru/post/980228/


All Articles