$result = array_reduce(array, function_name [, default ]);
The function takes two arguments: the running total, and the current
value being processed. It should return the new running total. For
instance, to add up the squares of the values of an array, use:
function add_up ($running_total, $current_value) {
$running_total += $current_value * $current_value;
return $running_total;
}
$numbers = array(2, 3, 5, 7);
$total = array_reduce($numbers, 'add_up');
// $total is now 87
The array_reduce( ) line makes these function
calls:
add_up(2,3)
add_up(13,5)
add_up(38,7)
The default argument, if provided, is a
seed value. For instance, if we change the call to
array_reduce( ) in the previous example to:
$total = array_reduce($numbers, 'add_up', 11);
The resulting function calls are:
add_up(11,2)
add_up(13,3)
add_up(16,5)
add_up(21,7)
If the array is empty, array_reduce( ) returns the
default value. If no default value is
given and the array is empty, array_reduce( )
returns NULL.