PHP | Ds\Set reduce() Function Last Updated : 22 Aug, 2019 Comments Improve Suggest changes Like Article Like Report 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. Syntax: 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: Program 1: php <?php // Declare new Set $set = new \Ds\Set([1, 2, 3, 4, 5]); echo("Set Elements\n"); print_r($set); // Callback function with reduce function echo("\nElement after performing operation\n"); var_dump($set->reduce(function($carry, $element) { return $carry + $element + 2; })); ?> Output: Set Elements Ds\Set Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Element after performing operation int(25) Program 2: php <?php // Declare new Set $set = new \Ds\Set([10, 20, 30, 40, 50]); echo("Original set elements\n"); print_r($set); $func_gfg = function($carry, $element) { return $carry * $element; }; echo("\nSet after reducing to single element\n"); // Use reduce() function var_dump($set->reduce($func_gfg, 10)); ?> Output: Original set elements Ds\Set Object ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 ) Set after reducing to single element int(120000000) Reference: https://www.php.net/manual/en/ds-set.reduce.php Create Quiz Comment R R_Raj Follow 0 Improve R R_Raj Follow 0 Improve Article Tags : Web Technologies PHP PHP-function PHP-ds_set Explore BasicsPHP Syntax4 min readPHP Variables5 min readPHP | Functions6 min readPHP Loops4 min readArrayPHP Arrays5 min readPHP Associative Arrays4 min readMultidimensional arrays in PHP5 min readSorting Arrays in PHP4 min readOOPs & InterfacesPHP Classes2 min readPHP | Constructors and Destructors5 min readPHP Access Modifiers4 min readMultiple Inheritance in PHP4 min readMySQL DatabasePHP | MySQL Database Introduction4 min readPHP Database connection2 min readPHP | MySQL ( Creating Database )3 min readPHP | MySQL ( Creating Table )3 min readPHP AdvancePHP Superglobals6 min readPHP | Regular Expressions12 min readPHP Form Handling4 min readPHP File Handling4 min readPHP | Uploading File3 min readPHP Cookies9 min readPHP | Sessions7 min read Like