array_reduce()

Anonymous contributor's avatar
Anonymous contributor
Published Aug 25, 2023
Contribute to Docs

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:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn PHP on Codecademy