I have recursive method in PHP merging values of array.
If array has more than 2 elements it sum up first two values to first one and removes second value. It does again and again until is only 1 value. Whole sum of these operations is result of function. However I get nesting error for that method:
PHP Fatal error: Uncaught Error: Maximum function nesting level of '256' reached, aborting
I think that problem lies in removing second element of array. It seems that it doesn't remove it properly.
function mergeList($A)
{
$arraySize = count($A);
if ($arraySize < 2) {
return 0;
} else {
$A[0] = $A[0] + $A[1];
unset($A[1]);
return $A[0] + mergeList($A);
}
}
Example of method:
$A = [1, 2, 3, 4];
Output should be 19, because [1,2,3,4] -> [3,3,4] -> [4,6] -> [10]
Sum of operations 3 -> 6 -> 10 = 19