PHP - Ds Map::diff() Function



The PHP Ds\Map::diff() function creates a new map by using keys that aren't in another map and returns the result of removing all keys from the current instance (say map) that are present in the given map.

If the keys in the first map are also present in the second map, this function returns an empty () map. For example map1 = (["1" => 1, "2" => 2]), and map2 = (["1" => 1, "2" => 2, "3" => 3]), so the map1->diff(map2) will be empty (), because map1 keys 1 and 2 are present in map2.

Syntax

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

public Ds\Map Ds\Map::diff( Ds\Map $map )

Parameters

This function accepts a "map" as a parameter, which is described below −

  • map − The other map contains keys to exclude in the resulting map.

Return value

This function returns the result of removing all keys from the current instance that are present in a given map.

Example 1

The following program demonstrates the usage of the PHP Ds\Map::diff() function −

<?php  
   $map1 = new \Ds\Map(["1" => 10, "2" => 20, "3" => 30]);               
   $map2 = new \Ds\Map(["3" => 30, "4" => 40, "5" => 50]);
   echo "The map1 elements are: \n";
   print_r($map1);
   echo "The map2 elements are: \n";
   print_r($map2);
   echo "The difference is: \n";
   print_r($map1->diff($map2));
?>

Output

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

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

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

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

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

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

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

)
The difference is:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => 10
        )

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

)

Example 2

Following is another example of the PHP Ds\Map::diff() function. We use this function to create a new map using the keys that are not present in this map (["2" => "Point", "3" => "Tutorials", "4" => "Tutorix"]) −

<?php  
   $map1 = new \Ds\Map(["1" => "Tutorials", "2" => "Point", "3" => "India"]);   
   $map2 = new \Ds\Map(["2" => "Point", "3" => "Tutorials", "4" => "Tutorix"]);
   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 "\nThe difference is: \n";
   #using diff() function
   print_r($map1->diff($map2));
?> 

Output

Once the above program is executed, it generates the following output −

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

The map2 elements are:
[2] = Point
[3] = Tutorials
[4] = Tutorix

The difference is:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => Tutorials
        )

)

Example 3

If both map has similar keys for the elements, then the difference of both maps will be empty, even if the values are different.

<?php  
   $map1 = new \Ds\Map(["1" => 'a', '2' => "b", '3' => 'c']);   
   $map2 = new \Ds\Map(["1" => 'd', '2' => "e", '3' => 'f']);
   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 "\nThe difference is: \n";
   #using diff() function
   print_r($map1->diff($map2));
?> 

Output

The above program displays the following output −

The map1 elements are:
[1] = a
[2] = b
[3] = c

The map2 elements are:
[1] = d
[2] = e
[3] = f

The difference is:
Ds\Map Object
(
)
php_function_reference.htm
Advertisements