Open In App

PHP | Ds\Map reduce() Function

Last Updated : 21 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax:

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:

Program 1:




<?php 
  
// Declare a new map
$map = new \Ds\Map(["a" => 1, "b" => 3, "c" => 5]); 
   
echo("Map Elements\n"); 
   
print_r($map); 
   
// Callback function with reduce function 
echo("\nElement after performing operation\n"); 
  
var_dump($map->reduce(function($carry, $key, $element) { 
    return $carry + $element + 2; 
})); 
   
?> 


Output:

Map Elements
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 3
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 5
        )

)

Element after performing operation
int(15)

Program 2:




<?php 
   
// Declare new Map elements
$map = new \Ds\Map(["a" => 10, "b" => 20,
            "c" => 30, "d" => 40, "e" => 50]); 
   
echo("Original map elements\n"); 
   
print_r($map); 
   
$func_gfg = function($carry, $key, $element) { 
    return $carry * $element
}; 
   
echo("\nMap after reducing to single element\n"); 
   
// Use reduce() function 
var_dump($map->reduce($func_gfg, 10)); 
   
?> 


Output:

Original map elements
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 10
        )

    [1] => Ds\Pair Object
        (
            [key] => b
            [value] => 20
        )

    [2] => Ds\Pair Object
        (
            [key] => c
            [value] => 30
        )

    [3] => Ds\Pair Object
        (
            [key] => d
            [value] => 40
        )

    [4] => Ds\Pair Object
        (
            [key] => e
            [value] => 50
        )

)

Map after reducing to single element
int(120000000)

Reference: https://www.php.net/manual/en/ds-map.reduce.php



Similar Reads

PHP | Ds\Map map() Function
The Ds\Map::map() function of the Map class in PHP is used to apply a callback function to a Map object. This returns the result of applying the callback function to each value present on the map. The function does not update the values in the original map, instead, it just returns the result of the updates without affecting the original values. Sy
2 min read
How to map, reduce and filter a Set element using JavaScript ?
The map(), reduce() and filter() are array functions that transform the array according to the applied function and return the updated array. They are used to write simple, short and clean codes for modifying an array instead of using the loops. map() method: It applies a given function on all the elements of the array and returns the updated array
2 min read
How to use map(), filter(), and reduce() in JavaScript ?
The map(), filter(), and reduce() are the array functions that allow us to manipulate an array according to our own logic and return a new array after applying the modified operations on it. JavaScript map() MethodThe map() method in JavaScript is used to create a new array by applying a function to each element of the original array. It iterates t
3 min read
Convert 2D Array to Object using Map or Reduce in JavaScript
Converting a 2D array to an object is a common task in JavaScript development. This process involves transforming an array where each element represents a key-value pair into an object where keys are derived from one dimension of the array and values from another. Problem Description:Given a 2D array where each inner array contains a key-value pair
2 min read
How to Convert Array into Array of Objects using map() & reduce() in JavaScript?
An array of objects can contain multiple objects with the same properties i.e. key-value pairs. But a map does not contain duplicate values which means if you convert an array of objects with duplicate objects into a map, it will contain only the unique key-value pairs. Below are the approaches to convert an array of objects to a map in JavaScript:
2 min read
PHP | Ds\Vector 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 below: $callback: This parameter holds the function
2 min read
PHP | Ds\Deque reduce() Function
The Ds\Deque::reduce() function is an inbuilt function in PHP which is used to reduce the Deque to a single element using callback function. Syntax: public Ds\Deque::reduce( $callback, $initial ) : mixed Parameters: This function accept two parameters as mentioned above and described below: callable: It contains the operation to be performed on the
2 min read
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. Syntax: mixed public Ds\Set::reduce ( callable $callback [, mixed $initial ] ) Parameters: This function accepts two parameters as mentioned above and described below: $callback: This para
2 min read
PHP | Ds\Sequence 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 mentioned above and described below: $callback: This param
2 min read
How to map array values without using map method in JavaScript ?
Array elements can be mapped by using looping methods in JavaScript. The map() method creates a new array with the results of the output of a function called for each array element. This can also be implemented using for loop in JavaScript. Approach: For this, we can create two arrays, in which one array contains the array elements that are to be m
2 min read