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:

<?php

$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 $numbers array that has three numbers 10, 20, and 30.
  • Second, define a variable $total and initialize it to zero.
  • Third, add up the numbers from the $numbers array to the $total variable iteratively using a foreach loop.
  • Finally, show the value of the $total variable.

Alternatively, you can use the array_reduce() function to achieve the same result without using the foreach statement:

<?php

$numbers = [10,20,30];

$total  = array_reduce($numbers, function ($previous, $current) {
    return $previous + $current;
});

echo $total; // 60Code 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:

<?php

$numbers = [10,20,30];

$total  = array_reduce(
    $numbers,
    fn ($previous, $current) => $previous + $current
);

echo $total; // 60Code 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:

  • $array is the input array that will be reduced to a single value.
  • $callback is a callback function that determines how the array should be reduced.
  • $initial is a value that the arrary_reduce() function uses at the beginning of the reducing process. The array_reduce() function returns $initial as the final result if the $array is 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 $carry holds the return value of the previous iteration. In the first iteration, it holds the value of the $initial instead.
  • The $item holds 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:

<?php

$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; // 155Code 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:

<?php

$carts = [];

$total = array_reduce(
    $carts,
    function ($prev, $item) {
        return $prev + $item['qty'] * $item['price'];
    },
    0
);

echo $total; // 155Code 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.
Did you find this tutorial useful?