Example 1 — Add Up Numbers
Reduce a list of numbers into one total.
<?php
$numbers = [1, 2, 3, 4];
$total = array_reduce($numbers, fn($carry, $item) => $carry + $item, 0);
echo $total, PHP_EOL; How It Works
- Create an array. It holds 1, 2, 3 and 4.
- Start the carry at 0. This is the starting total.
- Run the callback on each item. The carry goes 1, 3, 6 and then 10.
- Print the total. The final carry is the return value.

