PHP | Ds\Map merge() Function

The Ds\Map::merge() function is an inbuilt function in PHP which is used to return the result of adding all given associations.

Syntax:

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

Parameters: This function accepts single parameter $values which holds the traversable object or an array.

Return Value: This function returns the associating all keys of a given traversable object or array with their corresponding values, combined with the current instance.

Below programs illustrate the Ds\Map::merge() function in PHP:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
   
// Create new map
$map = new \Ds\Map(["a" => 12,
    "b" => 15, "c" => 18, "d" => 20]); 
   
// Merge the map element and display it 
print_r($map->merge(["a" => 1, "c" => 2, "f" => 3])); 
   
// Display the set element 
print_r($map
  
?> 

chevron_right


Output:

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

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

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

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

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

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

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

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

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

)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
   
// Create new map
$map = new \Ds\Map(["1" => "Geeks",
        "2" => "for", "3" => "Geeks"]); 
   
// Merge the map element and display it 
print_r($map->merge(["a" => "Computer",
        "b" => "Science", "c" => "Portal"])); 
  
?> 

chevron_right


Output:

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

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

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

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

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

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

)

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.