PHP array_reduce()
The array_reduce() method iterates through an array to produce a single value.
Syntax
array_reduce($array, $callback, $initial = null)
The first two parameters $array and $callback are both required, if no argument is given for the $initial parameter it will default to the first element of the $array argument.
The callback pseudo-code:
callback($carry, $item)
This is the function that will be applied to every element of the array, and it must return a value. To start, $carry is the $initial argument, or the first element of the $array argument if nothing was given for the $initial parameter.
The $item argument is simply the first element of the $array argument or its second element respectively. After the first iteration $carry will be the result returned from the function and $item will be the next element of the array.
After this loop has completed the array_reduce() function will return a single value of any type.
Example
The example below demonstrates how the array_reduce() method can be used to return the largest value in an array.
<?php$arrOfNumbers = [1, 6, 10, 14, 23];function highestNumber($carry, $item) {if ($item > $carry) return $item;return $carry;}printf("The largest value in the array is: %f", array_reduce($arrOfNumbers, "highestNumber"));?>
The output will be:
The largest value in the array is: 23
Codebyte Example
The following code is runnable and demonstrates the use of array_reduce() with an optional initial value:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours