PHP array_reduce() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_reduce() combines all the items of an array into a single value by running a function on each item.

The function receives the result so far and the current item, and returns the next result. It is often used for totals.

Syntax

Call array_reduce() with the values you need. Start with this line.

PHP
$total = array_reduce($numbers, fn($carry, $item) => $carry + $item, 0);
CallWhat it does
array_reduce([1, 2, 3], fn($c, $i) => $c + $i, 0)Returns 6, the sum.
array_reduce(["a", "b"], fn($c, $i) => $c . $i, "")Returns "ab".
array_reduce([], fn($c, $i) => $c + $i)Returns null, because there is no initial value.

Always give a sensible initial value, such as 0 for sums or an empty string for text. Otherwise the first step starts with null.

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

Parameters

ParameterDescription
arrayRequired. The array to reduce.
callbackRequired. A function taking ($carry, $item). It returns the new carry value.
initialOptional. The starting value of the carry. Default is null.

Return Value

Return ValueDescription
mixedThe final carry value, or the initial value if the array is empty.

PHP Examples

Example 1 — Add Up Numbers

Reduce a list of numbers into one total.

PHP
<?php
$numbers = [1, 2, 3, 4];

$total = array_reduce($numbers, fn($carry, $item) => $carry + $item, 0);

echo $total, PHP_EOL;

How It Works

  1. Create an array. It holds 1, 2, 3 and 4.
  2. Start the carry at 0. This is the starting total.
  3. Run the callback on each item. The carry goes 1, 3, 6 and then 10.
  4. Print the total. The final carry is the return value.

Example 2 — Empty Arrays and the Initial Value

What you get when there is nothing to reduce depends on the initial value.

PHP
<?php
$add = fn($carry, $item) => $carry + $item;

echo var_export(array_reduce([], $add), true), PHP_EOL;
echo array_reduce([], $add, 0), PHP_EOL;

How It Works

  1. Reduce an empty array with no initial value. The callback never runs.
  2. Get the default. The initial value is null, so the result is NULL.
  3. Add an initial value. This time we pass 0.
  4. Get a useful result. The result is 0, which is better for totals.

Example 3 — Total a Shopping Cart

Reduce an array of arrays by reading one field from each item.

PHP
<?php
$cart = [
    ["name" => "Pen", "price" => 2],
    ["name" => "Book", "price" => 10],
];

$total = array_reduce($cart, fn($sum, $item) => $sum + $item["price"], 0);

echo "Total: " . $total . PHP_EOL;

How It Works

  1. Create the cart. Each item has a name and a price.
  2. Receive each item. The callback gets the running sum and the current item.
  3. Read the price. $item["price"] is added to the sum.
  4. Print the total. 2 plus 10 is 12.

Frequently Asked Questions

It turns an array into a single value by calling a function on each item with the running result.
array_reduce(array $array, callable $callback, mixed $initial = null): mixed. The callback receives $carry and $item.
It is the result built up so far. At the first step it equals the initial value.
It returns the initial value, which is null if you did not pass one.
Use array_sum() for plain totals. array_reduce() is for custom logic, such as summing a field or building a string.

Next: array_replace()

array_replace() replaces the values of the first array with values from other arrays that .

array_replace() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful