⊗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.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline