PHP | Ds\Vector reduce() Function Last Updated : 22 Aug, 2019 Comments Improve Suggest changes Like Article Like Report The Ds\Vector::reduce() function is an inbuilt function in PHP which reduce the vector to a single value by applying operations in the callback function. Syntax: mixed public Ds\Vector::reduce( $callback, $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\Vector::reduce() function in PHP: Program 1: PHP <?php // Declare new Vector $vect = new \Ds\Vector([1, 2, 3, 4, 5]); echo("Vector Elements\n"); print_r($vect); // callback function with reduce function echo("\nElement after performing operation\n"); var_dump($vect->reduce(function($carry, $element) { return $carry + $element + 2; })); ?> Output: Vector Elements Ds\Vector Object ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Element after performing operation int(25) Program 2: PHP <?php // Declare new Vector $vect = new \Ds\Vector([10, 20, 30, 40, 50]); echo("Original vector elements\n"); print_r($vect); $func_gfg = function($carry, $element) { return $carry * $element; }; echo("\nVector after reducing to single element\n"); // Use reduce() function var_dump($vect->reduce($func_gfg, 10)); ?> Output: Original vector elements Ds\Vector Object ( [0] => 10 [1] => 20 [2] => 30 [3] => 40 [4] => 50 ) Vector after reducing to single element int(120000000) Reference: http://php.net/manual/en/ds-vector.reduce.php Comment More infoAdvertise with us Next Article PHP | DsMap reverse() Function B barykrg Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-ds_vector Similar Reads PHP | DsSet 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. Syntax: mixed public Ds\Set::reduce ( callable $callback [, mixed $initial ] ) Parameters: This function accepts two parameters as mention 2 min read PHP | DsVector reduce() Function The Ds\Vector::reduce() function is an inbuilt function in PHP which reduce the vector to a single value by applying operations in the callback function. Syntax: mixed public Ds\Vector::reduce( $callback, $initial ) Parameters: This function accepts two parameters as mentioned above and described be 2 min read PHP | DsMap reverse() Function The Ds/Map::reverse() function in PHP is used to in-place reverse the elements of a specified Map instance. That is, the function in-place reverses the order of elements present in the specified Map instance. Syntax: Ds\Map public Ds\Map::reverse ( int $position ) Parameter: This function does not a 2 min read PHP | DsSequence reduce() Function The Ds\Sequence::reduce() function is an inbuilt function in PHP which is used to reduce the sequence to a single value using a callback function. Syntax: mixed abstract public Ds\Sequence::reduce ( callable $callback [, mixed $initial ] ) Parameters: This function accepts two parameters as mentione 2 min read PHP array_reduce() Function 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 2 min read PHP | DsMap remove() Function The Ds\Map::remove() function is an inbuilt function in PHP which is used to remove and return a value by key. Syntax: mixed Ds\Map::remove( $key, $default ) Parameters: This function accepts two parameters as mentioned above and described below: $key: It holds the key value which need to remove. $d 2 min read PHP | DsDeque reverse() Function The Ds\Deque::reverse() function is an inbuilt function in PHP which is used to reverse the elements in the Deque in-place. Syntax: public Ds\Deque::reverse( void ) : void Parameters: This function does not accept any parameter. Return Value: This function does not return any value. Below programs i 2 min read PHP | DsVector reverse() Function The Ds\Vector::reverse() function is an inbuilt function in PHP which is used to reverse the vector elements in-place. Syntax: void public Ds\Vector::reverse( void ) Parameters: This function does not accepts any parameters. Return Value: This function does not return any value. Below programs illus 2 min read PHP | DsDeque reversed() Function The Ds\Deque::reversed() function is an inbuilt function in PHP which is used to return the copy of Deque which contains the elements in reversed order. Syntax: public Ds\Deque::reversed( void ) : Ds\Deque Parameters: This function does not accept any parameter. Return Value: This function returns a 2 min read PHP | DsVector reversed() Function The Ds\Vector::reversed() function is an inbuilt function in PHP which is used to reverse the elements of vector after copying the elements of original vector into a copy. Syntax: public Ds\Vector::reversed( void ) : Ds\Vector Parameters: This function does not accept any parameter. Return Value: Th 2 min read Like