Wether by recursion or iteration, everything in your $pre array will be added to the $to array. The method used to achieve this doesn't change the end result, so I really fail to see the point. Recursion adds overhead for function calls (even though they MAY be inlined by a compiler), and needs to allocate more memory.
# one way of doing it
function sumElements($to, $add) {
if (empty($add))
return $to;
$k = key($add);
$v = array_shift($add);
$to[$k] = (isset($to[$k]) ? $to[$k] : 0) + $v;
return sumElements($to, $add);
}
print_r(sumElements($to, $pre));
#another way of doing it
function sumElements(&$to, $add) {
if (!empty($add)) {
$k = key($add);
$v = array_shift($add);
$to[$k] = (isset($to[$k]) ? $to[$k] : 0) + $v;
sumElements($to, $add);
}
}
sumElements($to, $pre);
print_r($to);