The PHP function array_reduce() takes a callback function that accepts a value carried over each iteration and the current item in the array, and reduces an array to a single value. Which code sample will sum and output the values in the provided array?

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
  • [ ]
    1 <?php
    2 echo array_reduce([1, 2, 5, 10, 11], function ($item, $carry) {
    3 $carry = $carry + \$item;
    4 });
    5?>
  • [ ]
    1 <?php
    2 echo array_reduce([1, 2, 5, 10, 11], function ($carry, $item) {
    3 return $carry = $item + \$item;
    4 });
    5?>
  • [ ]
    1 <?php
    2 array_reduce([11 2, 5, 10, 11], function ($item, $carry) {
    3 echo $carry + $item;
    4 });
    5?>
  • [ ]
    1 <?php –CORRECT
    2 echo array_reduce([1, 2, 5, 10, 11], function ($carry, $item) {
    3 return $carry += $item;
    4 });
    5?>
0/5 (0 Reviews)

Like this article?

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest

Related Posts