PHP array_reduce() Function

PHP array_reduce() Function

The array_reduce() function in PHP reduces the array to a single value using by applying a callback function to the elements of the array.

Syntax

array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] )

Parameter

array(required)- This parameter represents the input array.

callback (required)- This parameter signifies user defined function that is used to hold the value of the $array.

initial(optional)- This parameter represents the value to be sent to the function.

Return

This function returns the resulting reduced value. It can be of any type int, float or string. It returns null if the array is empty and the “initial” parameter is not passed.

Example 1

 

Output

Reducing the array to 8

Example 2

 

Output

Reducing the array to 240

Example 3

 

Output

Reducing the array to 'No data to reduce'