The Ds\Map::reduce() function is an inbuilt function in PHP which is used to reduce the map to a single value by applying operations using the callback function.
mixed public Ds\Map::reduce( $callback, $initial )
Parameter: 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 three arguments which are listed below:
- carry: It holds the return value of the previous callback, or initial if it’s the first iteration.
- key: It holds the key of the current iteration.
- value: It holds the value of 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\Map::reduce() function in PHP:
Map Elements Ds\Map Object (  => Ds\Pair Object ( [key] => a [value] => 1 )  => Ds\Pair Object ( [key] => b [value] => 3 )  => Ds\Pair Object ( [key] => c [value] => 5 ) ) Element after performing operation int(15)
Original map elements Ds\Map Object (  => Ds\Pair Object ( [key] => a [value] => 10 )  => Ds\Pair Object ( [key] => b [value] => 20 )  => Ds\Pair Object ( [key] => c [value] => 30 )  => Ds\Pair Object ( [key] => d [value] => 40 )  => Ds\Pair Object ( [key] => e [value] => 50 ) ) Map after reducing to single element int(120000000)
- PHP | Ds\Vector reduce() Function
- PHP | Ds\Deque reduce() Function
- PHP | Ds\Set reduce() Function
- PHP | Ds\Sequence reduce() Function
- Ways To Reduce The Loading Time Of Website | Set 1
- Ways To Reduce The Loading Time Of Your Website | Set 2
- Underscore.js | _.reduce() Method
- Reduce the size of an icon during the animation
- TypeScript | Array reduce() Method
- Lodash _.reduce() Method
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.