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

mixed array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] )

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.

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

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

echo $sum; // outputs: 15

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.

$strings = ["Hello", "World", "!"];

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

echo $concatenatedString; // outputs: Hello World !

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.

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

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

echo $product; // outputs: 120

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHParray_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

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.