# Add array values ​​recursively

There is an array:

``````\$arr = array(21,2,3,5,6);
``````

We need to get something out of it:

``````\$arr = array(21,23,26,31,37);
``````

I.e. add each value with the previous one (naturally, except for the 1st). Tell me how you can implement this task with the least “perversions”?

P.S. without `foreach()`and without `count()`.

P.P.S. array keys start at 1 at the input, they must be the same at the output.

## Answer 1, authority 100%

``````\$arr = array(0, 21, 2, 3, 5, 6);
\$i = 1;while(isset(\$arr[\$i])){
\$arr[\$i] = \$arr[\$i] + \$arr[\$i-1];
++\$i;
}
``````

## Answer 2, authority 33%

``````\$arr = array(1=>21, 2, 3, 5, 6);
mult(\$arr, \$newArr);
print_r(\$newArr);
function mult(&\$oldArray, &\$newArray, &\$prev = 0) {
\$last = array_pop(\$oldArray);
if(empty(\$oldArray)) {
\$prev = \$last;
\$newArray = array(1=>\$prev);
return;
}
mult(\$oldArray, \$newArray, \$prev);
\$prev = \$prev + \$last;
array_push(\$newArray, \$prev);
}
``````

Probably it can be further simplified. Plus, I don’t know how it will behave with non-numeric keys. And, also, I absolutely do not guarantee that the algorithm is much faster than `foreach`. But you asked for recursion. Although I would not say about the absence of perversions.

There is also an option without recursion:

``````function mult2(\$oldArray) {
\$elem = current(\$oldArray);
\$newArray = array(key(\$oldArray)=>\$elem);
while(\$elem = next(\$oldArray)) {
\$newArray[key(\$oldArray)] = \$elem + end(\$newArray);
}
return \$newArray;
}
\$arr = array('a'=>21, 'b'=>2, 1=>3, 5, 6);
``````

Moreover, this option works very well with any type of keys.

## Answer 3, authority 33%

Optional:

``````<?
function recursive( \$arr, \$res, \$last ) {
if ( empty(\$arr) ) return \$res;
\$sum = \$last + array_pop( \$arr );
\$res[] = \$sum;
return recursive( \$arr, \$res, \$sum );
}
\$arr = array( 21, 2, 3, 5, 6 );
print_r( \$arr );
\$calc = recursive( array_reverse( \$arr ), array( 1=> arr ), arr );
print_r( \$calc );
?>
``````

either like this (changes the original array):

``````<?
function recursive( \$arr ) {
\$prev = current(\$arr);
if ( !next(\$arr) ) return \$arr;
\$arr[ key(\$arr) ] += \$prev;
return recursive(\$arr);
}
\$arr = array( 1=>21, 3, 5, 6 );
print_r( \$arr );
\$calc = recursive( \$arr );
print_r( \$calc );
?>
``````

either via iteration:

``````<?
function iter_while(\$arr) {
\$prev = current(\$arr);
while( next( \$arr ) ){
\$arr[ key( \$arr ) ] += \$prev;
\$prev = current(\$arr);
}
return \$arr;
}
\$arr = array( 1=>21, 3, 5, 6 );
print_r( \$arr );
\$calc = iter_while( \$arr );
print_r( \$calc );
?>
``````

``````<?php
\$arr = array(1 => 22, 2=> 3, 3 => 6);
\$result = \$arr;
\$totalcount = \$arr;
for(\$i = 1; \$i < 1000; \$i++)
{
if (isset(\$arr[\$i +1 ]))
{
\$result[\$i + 1] = \$totalcount + \$arr[\$i + 1];
\$totalcount = \$result[\$i + 1];
}
else
{
break;
}
}
print_r(\$result);
?>
``````

Well, here’s a quick sketch, probably better. There is only my array)

``````\$arr = array(1,2,3,4,5,6,7);
while(count(\$arr)>1) \$arr+=array_pop(\$arr);
``````

Recursive is like this:

``````\$a = array(1=>21, 2, 3, 5, 6);
function sum(&\$item, \$key, &\$s){
\$s = \$item += \$s;
}
array_push(\$a,\$a);
array_walk_recursive(\$a, 'sum', \$s=0);
array_pop(\$a);
print("a = "); print_r(\$a);
``````

P.S. There is an easier solution

``````\$a = array(1=>21, 2, 3, 5, 6);
\$s=0;
array_walk(\$a, function (&\$item, \$key) use(&\$s){
\$s = \$item += \$s;
});
print("a = "); print_r(\$a);
``````

with the same result:
`a = Array (  => 21  => 23  => 26  => 31  => 37 )`