← All built-ins / array_reduce()
array_reduce()
array_reduce(...$args): mixed
Parameters
| Name | Type | Description |
|---|---|---|
| $a | array | array to fold |
| $fn | closure | fn($carry, $item) => new $carry |
| $initial | mixed | starting valueoptional |
Return value
The final carry after folding left-to-right.
Example
$sum = array_reduce([1, 2, 3, 4], fn($c, $x) => $c + $x, 0); echo $sum;
10