PHP - Ds Map::merge() Function



The PHP Ds\Map::merge() function is used to retrieve the result of adding all given associations, which are the key and value pairs of the map elements. This function will not affect the current instance.

If the keys are equal in both maps, the values of the current instance will be overwritten by those provided.

Syntax

Following is the syntax of the PHP Ds\Map::merge() function −

public Ds\Map Ds\Map::merge( mixed $values )

Parameters

Following is the parameter of this function −

  • values − The values (i.e. traversal object or an array) need to be merged with the current instance.

Return value

This function returns the result of associating all keys of a given traversable object or array with their corresponding values, merged with the current instance.

Example 1

The following is the basic example of the PHP Ds\Map::merge() function −

<?php  
   $map1 = new \Ds\Map(["0" => 1, "1" => 2, "2" => 3]);
   $map2 = new \Ds\Map(["3" => 4, "4" => 5, "5" => 6]);
   echo "The map1 elements are: \n";
   print_r($map1);
   echo "The map2 elements are: \n";
   print_r($map2);
   echo "New map after merge: \n";
   #using merge() function
   $new_map = $map1->merge($map2);
   print_r($new_map);
?>

Output

The above program produces the following output −

The map1 elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 1
        )

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

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

)
The map2 elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 3
            [value] => 4
        )

    [1] => Ds\Pair Object
        (
            [key] => 4
            [value] => 5
        )

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

)
New map after merge:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 1
        )

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

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

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

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

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

)

Example 2

Following is another example of the PHP Ds\Map::merge() function. We use this function to retrieve the result of adding all the given associations (["1" => "Tutorials", "2" => "Point", "3" => "India"]) and (["a" => "Tutorix", "b" => "Hyderabad", "c" => "Madhapur"]) −

<?php  
   $map1 = new \Ds\Map(["1" => "Tutorials", "2" => "Point", "3" => "India"]);
   $map2 = new \Ds\Map(["a" => "Tutorix", "b" => "Hyderabad", "c" => "Madhapur"]);
   echo "The map1 elements are: \n";
   foreach($map1 as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }
   echo "\nThe map2 elements are: \n";
   foreach($map2 as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }   
   echo "\nNew map after merge: \n";
   #using merge() function
   $new_map = $map1->merge($map2);
   foreach($new_map as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }
?> 

Output

After executing the above program, it will display the following output −

The map1 elements are:
[1] = Tutorials
[2] = Point
[3] = India

The map2 elements are:
[a] = Tutorix
[b] = Hyderabad
[c] = Madhapur

New map after merge:
[1] = Tutorials
[2] = Point
[3] = India
[a] = Tutorix
[b] = Hyderabad
[c] = Madhapur

Example 3

If the keys are equal in both maps, the values of the current instance will be overwritten by those provided where the keys are equal −

<?php  
   $map1 = new \Ds\Map(['a', 'b', 'c']);
   $map2 = new \Ds\Map(['d', 'e', 'f']);
   echo "The map1 elements are: \n";
   print_r($map1);
   echo "The map2 elements are: \n";
   print_r($map2);
   echo "New map after merge: \n";
   #using merge() function
   $new_map = $map1->merge($map2);
   print_r($new_map);
?>

Output

Once the above program is executed, it will generate the following output −

The map1 elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => a
        )

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

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

)
The map2 elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => d
        )

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

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

)
New map after merge:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => d
        )

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

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

)
php_function_reference.htm
Advertisements