![]() |
VOOZH | about |
This inbuilt function of PHP is used to reduce the elements of an array into a single value that can be of float, integer or string value. The function uses a user-defined callback function to reduce the input array.
Syntax:
array_reduce($array, own_function, $initial)
Parameters:
The function takes three arguments and are described below:
Return Value: This function returns the reduced result. It can be of any type int, float or string.
Examples:
Input : $array = (15, 120, 45, 78) $initial = 25 own_function() takes two parameters and concatenates them with "and" as a separator in between Output : 25 and 15 and 120 and 45 and 78 Input : $array = array(2, 4, 5); $initial = 1 own_function() takes two parameters and multiplies them. Output : 40
In this program, we will see how an array of integer elements is reduced to a single string value. We also passed the initial element of our choice.
Output:
Initial and 15 and 120 and 45 and 78
In the below program, the array_reduce reduces the given array to the product of all the elements of the array using the own_function().
Output:
80000
Reference:
https://www.php.net/manual/en/function.array-reduce.php