PHP | Ds\Set reduce() Function
The Ds\Set::reduce() function is an inbuilt function in PHP which is used to reduce the set to a single value by applying operations using the callback function.
mixed public Ds\Set::reduce ( callable $callback [, mixed $initial ] )
Parameters: This function accepts two parameters as mentioned above and described below:
- $callback: This parameter holds the function which contains the operation on the elements and store carry. This callback function contains two arguments, carry and value, where carry is the value returned by the function and value is the value of the element at the current iteration.
- $initial: This parameter holds the initial value of carry, which can be NULL.
Return value: This function returns the final value returned by the callback function.
Below programs illustrate the Ds\Set::reduce() function in PHP:
Set Elements Ds\Set Object (  => 1  => 2  => 3  => 4  => 5 ) Element after performing operation int(25)
Original set elements Ds\Set Object (  => 10  => 20  => 30  => 40  => 50 ) Set after reducing to single element int(120000000)