PHP array_reduce Function
last modified March 13, 2025
The PHP array_reduce
function iteratively reduces an array to a
single value using a callback function. It's powerful for aggregation.
Basic Definition
The array_reduce
function processes array elements sequentially.
It applies a callback to combine elements into a single accumulated result.
Syntax: array_reduce(array $array, callable $callback, mixed $initial = null): mixed
.
The callback receives the carry (accumulator) and current item value.
Summing Array Elements
This example demonstrates how to sum all elements in an array using reduction.
<?php declare(strict_types=1); $numbers = [1, 2, 3, 4, 5]; $sum = array_reduce($numbers, function($carry, $item) { return $carry + $item; }, 0); echo "The sum is: $sum";
The callback adds each element to the accumulated sum. We start with 0 as initial value. The result is the total sum of all array elements.
Concatenating Strings
Reduce can combine array elements into a single string with a separator.
<?php declare(strict_types=1); $words = ["Hello", "world", "from", "PHP"]; $sentence = array_reduce($words, function($carry, $item) { return $carry ? "$carry $item" : $item; }, ""); echo $sentence;
This builds a sentence by concatenating words with spaces. The initial empty string prevents leading space. Each iteration adds the next word.
Finding Maximum Value
Implement a max function using array_reduce to find the largest value.
<?php declare(strict_types=1); $numbers = [42, 17, 89, 23, 56]; $max = array_reduce($numbers, function($carry, $item) { return $item > $carry ? $item : $carry; }, PHP_INT_MIN); echo "Maximum value: $max";
The callback compares each element with the current maximum. We start with the smallest possible integer. The result is the largest array element.
Flattening Nested Arrays
Use array_reduce to flatten a two-dimensional array into one dimension.
<?php declare(strict_types=1); $matrix = [[1, 2], [3, 4], [5, 6]]; $flat = array_reduce($matrix, function($carry, $item) { return array_merge($carry, $item); }, []); print_r($flat);
This merges all subarrays into one. We start with an empty array. Each iteration merges the current subarray with the accumulated result.
Counting Occurrences
Count how many times each value appears in an array using reduction.
<?php declare(strict_types=1); $fruits = ["apple", "banana", "apple", "orange", "banana", "apple"]; $counts = array_reduce($fruits, function($carry, $item) { $carry[$item] = ($carry[$item] ?? 0) + 1; return $carry; }, []); print_r($counts);
This builds an associative array counting each fruit. The null coalescing operator handles first occurrences. The result shows each fruit's count.
Best Practices
- Initial Value: Always provide a meaningful initial value.
- Type Consistency: Ensure callback returns match initial type.
- Performance: For large arrays, consider simpler loops.
- Readability: Use named functions for complex logic.
- Error Handling: Validate array contents before reduction.
Source
PHP array_reduce Documentation
This tutorial covered PHP's array_reduce
with practical examples
showing its versatility for array aggregation and transformation.
Author
List all PHP Array Functions.