PHP array_reduce() Function

This function applies iteratively the function function to the elements of the array, so as to reduce the array to a single value.

Note: If the array is empty and initial is not passed, this function returns NULL.

Returns the resulting value

ParameterDescription
arrayRequired. Specifies an array
myfunctionRequired. Specifies the name of the function
initialOptional. Specifies the initial value to send to the function

Syntax

 array_reduce(array,myfunction,initial) 

Example

function myfunction($s1,$s2)
{
return $s1+$s2;
}
$array=array(20,10,15);
print_r(array_reduce($array,"myfunction",5));

Output

50

Leave a Reply

Your email address will not be published. Required fields are marked *