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?

Advertisement

  • 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
    2 echo array_reduce([1, 2, 5, 10, 11], function ($carry, $item) {
    3 return $carry += $item;
    4 });
    5?>
Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Leave a Comment


Share via
Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.