Arrays and Iteration
Array Reducing
Reducing combines many array values into one result.
Array Reducing
array_reducing.php
<?php
$start = ;
$total = $start;
foreach ([1, 2, 3, 4] as $value) {
$total = $total + $value;
}
$difference = $total - $start;
echo "start=" . $start . "\n";
echo "total=" . $total . "\n";
echo "difference=" . $difference . "\n";
<?php
$start = ;
$total = $start;
foreach ([1, 2, 3, 4] as $value) {
$total = $total + $value;
}
$difference = $total - $start;
echo "start=" . $start . "\n";
echo "total=" . $total . "\n";
echo "difference=" . $difference . "\n";
<?php
$start = ;
$total = $start;
foreach ([1, 2, 3, 4] as $value) {
$total = $total + $value;
}
$difference = $total - $start;
echo "start=" . $start . "\n";
echo "total=" . $total . "\n";
echo "difference=" . $difference . "\n";
accumulator
An accumulator stores the running result while a loop combines values.