Functional Helpers
Reduce Total
array_reduce carries one accumulated value across the whole array.
Reduce Total
reduce_total.php
<?php
function add_total($carry, $value) {
return $carry + $value;
}
$start = ;
$values = [2, 3, 4];
$total = array_reduce($values, "add_total", $start);
echo "start=" . $start . "\n";
echo "total=" . $total . "\n";
<?php
function add_total($carry, $value) {
return $carry + $value;
}
$start = ;
$values = [2, 3, 4];
$total = array_reduce($values, "add_total", $start);
echo "start=" . $start . "\n";
echo "total=" . $total . "\n";
<?php
function add_total($carry, $value) {
return $carry + $value;
}
$start = ;
$values = [2, 3, 4];
$total = array_reduce($values, "add_total", $start);
echo "start=" . $start . "\n";
echo "total=" . $total . "\n";
accumulated result
A reducer callback receives the current total and the next value.