Definition
The array_reduce() function iteratively reduce the array to a single value using a callback function.
Syntax
array_reduce(array, function, initial)
Parameters
Parameter | Description |
---|---|
array |
Required. Specifies an array. |
function |
Required. Specifies the name of the function. |
initial |
Optional. Specifies the initial value to send to the function. |
Example
<?php
// Example 1
function sum($x, $y){
$x += $y;
return $x;
}
$numbers = array(1, 2, 3, 4, 5);
var_dump(array_reduce($numbers, "sum"));
echo "<br>";
// Example 2
$empty = array();
var_dump(array_reduce($empty, "sum", "No data to process!"));