PHP array_reduce
Summary: in this tutorial, you will learn how to use the PHP array_reduce() function to reduce an array to a single value.
Introduction to the PHP array_reduce function
The array_reduce() function reduces an array to a single value using a callback function. It’s easier to understand the array_reduce() function by example.
The following example calculate the sum of all numbers in an array:
$numbers = [10,20,30];
$total = 0;
foreach ($numbers as $number) {
$total += $number;
}
echo $total; // 60
Code language: HTML, XML (xml)
How it works.
- First, define the
$numbersarray that has three numbers 10, 20, and 30. - Second, define a variable
$totaland initialize it to zero. - Third, add up the numbers from the
$numbersarray to the$totalvariable iteratively using aforeachloop. - Finally, show the value of the
$totalvariable.
Alternatively, you can use the array_reduce() function to achieve the same result without using the foreach statement:
$numbers = [10,20,30];
$total = array_reduce($numbers, function ($previous, $current) {
return $previous + $current;
});
echo $total; // 60
Code language: HTML, XML (xml)
The array_reduce() function accepts an array and a callback function. It reduces the $numbers array to a single value using the callback function.
Since PHP 7.3, you can use an arrow function rather than an anonymous function as the callback function like this:
$numbers = [10,20,30];
$total = array_reduce(
$numbers,
fn ($previous, $current) => $previous + $current
);
echo $total; // 60
Code language: HTML, XML (xml)
PHP array_reduce() function syntax
The following shows the array_reduce() function’s syntax:
array_reduce ( array $array , callable $callback , mixed $initial = null ) : mixedCode language: PHP (php)
The array_reduce() function has the following parameters:
$arrayis the input array that will be reduced to a single value.$callbackis a callback function that determines how the array should be reduced.$initialis a value that thearrary_reduce()function uses at the beginning of the reducing process. The array_reduce() function returns$initialas the final result if the$arrayis empty.
If the input array is empty or the $initial is ommited, the array_reduce() function returns null.
The $callback function is often called a reducer. It’s where you decide the logic for reducing the array elements. The callback function has the following signature:
callback ( mixed $carry , mixed $item ) : mixedCode language: PHP (php)
The callback function accepts two arguments:
- The
$carryholds the return value of the previous iteration. In the first iteration, it holds the value of the$initialinstead. - The
$itemholds the value of the current iteration.
PHP array_reduce function example
The following example uses the array_reduce() function to calculate the total items in a shopping cart:
$carts = [
['item' => 'A', 'qty' => 2, 'price' => 10],
['item' => 'B', 'qty' => 3, 'price' => 20],
['item' => 'C', 'qty' => 5, 'price' => 30]
];
$total = array_reduce(
$carts,
function ($prev, $item) {
return $prev + $item['qty'] * $item['price'];
}
);
echo $total; // 155
Code language: HTML, XML (xml)
If the carts array is empty, you’ll get the total as null.
To return zero instead of null, you pass the initial argument as zero to the array_reduce() function like this:
$carts = [];
$total = array_reduce(
$carts,
function ($prev, $item) {
return $prev + $item['qty'] * $item['price'];
},
0
);
echo $total; // 155
Code language: HTML, XML (xml)
Since the $carts array is empty, the total is zero.
Summary
- Use the PHP
array_reduce()function to reduce an array to a single value using a callback function.