How PHP array_reduce Processes Arrays
This article explains how the array_reduce() function
works in PHP to reduce an array to a single value. You will learn about
its syntax, how the callback function iteratively processes each
element, and how the optional initial value affects the final output,
complete with practical code examples.
Understanding array_reduce()
The array_reduce() function in PHP is a built-in tool
used to iteratively reduce an array to a single scalar value (such as an
integer, string, float, or boolean) or even a new array. It does this by
applying a callback function to each element of the array.
Syntax
array_reduce(array $array, callable $callback, mixed $initial = null): mixed$array: The input array to be processed.$callback: The function that defines how the array elements are reduced.$initial(Optional): The starting value used at the beginning of the process. If the array is empty, this is the value returned.
How the Process Works (Step-by-Step)
The array_reduce() function processes an array
sequentially from the first element to the last.
The Callback Function Arguments
The callback function requires two arguments: 1.
$carry: This holds the return value of the
previous iteration. On the very first iteration, it holds the
$initial value (or null if no initial value is
provided). 2. $item: This holds the value
of the current element in the array during the loop.
Execution Flow
- Initialization: PHP sets the accumulator
(
$carry) to the$initialvalue. - First Iteration: PHP passes the
$carryand the first element of the array ($item) into the callback function. The callback processes these inputs and returns a new value, which becomes the updated$carry. - Subsequent Iteration: PHP passes the updated
$carryand the next element in the array to the callback function. The return value updates$carryagain. - Completion: This loop repeats until every element
in the array has been processed. The final value of
$carryis then returned as the output ofarray_reduce().
Code Examples
Example 1: Summing an Array of Numbers
This example demonstrates how to use array_reduce() to
sum all numbers in an array, starting with an initial value of
0.
$numbers = [1, 2, 3, 4, 5];
$sum = array_reduce($numbers, function($carry, $item) {
return $carry + $item;
}, 0);
echo $sum; // Output: 15Step-by-step processing for this example: *
Start: $carry is set to 0
(the initial value). * Iteration 1: $carry
(0) + $item (1) = 1. New $carry
is 1. * Iteration 2: $carry
(1) + $item (2) = 3. New $carry
is 3. * Iteration 3: $carry
(3) + $item (3) = 6. New $carry
is 6. * Iteration 4: $carry
(6) + $item (4) = 10. New $carry
is 10. * Iteration 5: $carry
(10) + $item (5) = 15. New $carry
is 15. * End: Returns 15.
Example 2: Concatenating Strings
You can also use array_reduce() to join array elements
into a formatted string.
$words = ['PHP', 'is', 'a', 'popular', 'language'];
$sentence = array_reduce($words, function($carry, $item) {
return $carry . ' ' . $item;
});
echo trim($sentence); // Output: PHP is a popular language(Note: Because no initial value was provided in Example 2, the
first iteration uses null as the starting
$carry value).