⊗ppPmRcAS 219 of 447 menu

Sum of Array Elements Using Recursion in PHP

Let's now not output the elements of the array to the screen, but find the sum of the elements of this array:

<?php function getSum($arr) { $sum = array_shift($arr); if (count($arr) !== 0) { $sum += getSum($arr); } return $sum; } var_dump(getSum([1, 2, 3])); ?>

Given an array:

<?php $arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]; ?>

Using recursion, find the sum of the elements of this array.

byenru