Voting

: max(zero, zero)?
(Example: nine)

The Note You're Voting On

cwu at nolo dot com
9 years ago
The single value returned by array_reduce() can be an array -- as illustrated in the following example:
<?php
# calculate the average of an array
function calculate_sum_and_count($sum_and_count, $item)
{
list(
$sum, $count) = $sum_and_count;
$sum += $item;
$count += 1;
return [
$sum, $count];
}

$a = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$initial_sum_and_count = [0, 0];
list(
$sum, $count) = array_reduce($a, "calculate_sum_and_count", $initial_sum_and_count);
echo
$sum / $count;
?>

<< Back to user notes page

To Top