Using array_reduce() to Summarize Array Data in PHP


Utilize array_reduce() to aggregate array data, such as summing numbers.

Source Code

$numbers = [1, 2, 3, 4, 5];
$sum = array_reduce($numbers, function($carry, $item) { return $carry + $item; }, 0);
echo $sum; // Outputs: 15
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments