Backend Development 5 min read

Using PHP's array_reduce() Function: Syntax, Parameters, and Practical Examples

This article explains PHP's array_reduce() function, detailing its syntax, parameter meanings, and demonstrating how to sum numbers, concatenate strings, and calculate products through clear code examples that illustrate reducing an array to a single value.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's array_reduce() Function: Syntax, Parameters, and Practical Examples

PHP provides powerful built‑in functions for array manipulation, and array_reduce() is one of the most versatile, allowing developers to iteratively reduce an array to a single value using a callback function.

array_reduce() Function Syntax

<code>mixed array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] )</code>

Parameter Description

$array : The input array to be processed.

$callback : A callable that receives the previous iteration result and the current array element, returning the new accumulated value.

$initial (optional): The initial value for the reduction; if omitted, the first array element is used as the initial value.

Using array_reduce()

Example 1: Summing Array Elements

To sum a list of numbers, pass the array and a simple callback that adds the carry and the current item.

<code>$numbers = [1, 2, 3, 4, 5];

$sum = array_reduce($numbers, function($carry, $item) {
    return $carry + $item;
});

echo $sum; // outputs: 15</code>

Example 2: Concatenating Array Elements into a String

Array elements can also be joined into a single string by concatenating the carry with each item.

<code>$strings = ["Hello", "World", "!"];

$concatenatedString = array_reduce($strings, function($carry, $item) {
    return $carry . " " . $item;
});

echo $concatenatedString; // outputs: Hello World !</code>

Example 3: Calculating the Product of Array Elements

For more complex reductions, such as computing a product, provide an initial value of 1 to avoid a NULL start.

<code>$numbers = [1, 2, 3, 4, 5];

$product = array_reduce($numbers, function($carry, $item) {
    return $carry * $item;
}, 1);

echo $product; // outputs: 120</code>

These examples demonstrate how array_reduce() offers a concise and powerful way to process arrays, reducing them to a single result through custom logic defined in the callback.

Summary

array_reduce() iteratively reduces array elements to a single value using a user‑defined callback.

The callback receives the previous result and the current element.

An optional initial value can be supplied to set the starting accumulator.

backend-developmentPHPCode Examplearray_reduce
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.