PHP array_reduce() Function

Reduce an array to a single value with a callback

Try array_reduce() - edit and run
Output
Sum: 15
Product: 120

What does array_reduce() do?

array_reduce() walks through the array and calls $callback($carry, $item) for each element, where $carry is the value returned by the previous call. The final $carry is returned. It is useful for sums, totals, grouping and building lookup tables.

Syntax

array_reduce(array $array, callable $callback, mixed $initial = null): mixed

Parameters

  • $array - The array to reduce.
  • $callback - Function fn($carry, $item) returning the new carry value. It does not receive keys.
  • $initial - Starting value of $carry. Defaults to null; it is also the return value for an empty array.

Return value

The final carry value, or $initial if the array is empty.

array_reduce() examples

Every example below shows the exact output it produces. Paste any of them into the editor above (or the online compiler) to run it.

1. Sum and product

<?php
$numbers = [1, 2, 3, 4, 5];
$sum = array_reduce($numbers, fn($carry, $n) => $carry + $n, 0);
$product = array_reduce($numbers, fn($carry, $n) => $carry * $n, 1);
echo "Sum: $sum\n";
echo "Product: $product\n";

Output:

Sum: 15
Product: 120

2. Shopping cart total

<?php
$cart = [
    ["item" => "Book", "price" => 12.50, "qty" => 2],
    ["item" => "Pen", "price" => 1.25, "qty" => 4],
];
$total = array_reduce($cart, fn($sum, $line) => $sum + $line["price"] * $line["qty"], 0);
echo "Total: " . number_format($total, 2) . "\n";

Output:

Total: 30.00

3. Group rows by a field

<?php
$people = [
    ["name" => "Ann", "team" => "red"],
    ["name" => "Ben", "team" => "blue"],
    ["name" => "Cara", "team" => "red"],
];
$byTeam = array_reduce($people, function ($groups, $person) {
    $groups[$person["team"]][] = $person["name"];
    return $groups;
}, []);
print_r($byTeam);

Output:

Array
(
    [red] => Array
        (
            [0] => Ann
            [1] => Cara
        )

    [blue] => Array
        (
            [0] => Ben
        )

)

4. Why you should pass an initial value

<?php
var_dump(array_reduce([], fn($c, $n) => $c + $n));
var_dump(array_reduce([], fn($c, $n) => $c + $n, 0));

Output:

NULL
int(0)

Common mistakes and gotchas

  • Missing initial value: without it the first carry is null and an empty array returns null.
  • Forgetting to return: a multi-line closure must return $carry;, otherwise the next call receives null.
  • Simple sums: array_sum() and array_product() are shorter and faster for plain numbers.

PHP array_reduce() FAQ

How do I access the key in array_reduce()?

The callback only gets values. Reduce over array_keys($arr) and read $arr[$key] inside, or use a foreach loop.

When should I use array_reduce() instead of foreach?

When you are combining all elements into one value (a total, a string, a grouped array). For anything with side effects, a loop is clearer.

What does $carry mean?

It is the accumulator: the value returned by the previous callback call, starting with $initial.