Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Map::xor() Function



The PHP Ds\Map::xor() function is used to create a new map by using the keys in the current instance and another map but not of both.

Which means that, if the values are equal in both maps, then the map returned by this function will be empty ([]). For example, map1 = ([1, 2, 3]), and map2 = ([1, 2, 3]), the map1->xor(map2) = ([]).

Following is the formula to calculate the XOR of two maps −

A XOR B = (A Union B) - (A Intersection B)

Here, A is considered as the first map, and B is considered as the second map.

Syntax

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

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

Parameters

Following is the parameter of this function −

  • map − The other map holds the set of values.

Return value

This function returns the XOR of two maps, exclusive OR.

Example 1

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

<?php  
   $map1 = new \Ds\Map([1, 2, 3]);  
   $map2 = new \Ds\Map([2, 3, 4, 5]);
   echo "The map1 elements are: ";
   foreach($map1 as $key=>$value){
	   echo $value." ";
   }
   echo "\nThe map2 elements are: ";
   foreach($map2 as $key=>$value){
	   echo $value." ";
   }
   echo("\nThe xor of both map: \n");
   #using xor() method  
   print_r ($map1->xor($map2));
?>

Output

The above program produces the following output −

The map1 elements are: 1 2 3
The map2 elements are: 2 3 4 5
The xor of both map:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 3
            [value] => 5
        )

)

Example 2

Following is another example of the PHP Ds\Map::xor() function. We use this method to retrieve a new map containing values in this map (["Hello", "Tutorials", "Point", "India"]) and another map (["Tutorials", "Point", "India"]), but not in both.

<?php  
   $map1 = new \Ds\Map(["Hello", "Tutorials", "Point", "India"]);  
   $map2 = new \Ds\Map(["Tutorials", "Point", "India"]);
   echo "The map1 elements are: \n";
   print_r($map1);
   echo "The map2 elements are: \n";
   print_r($map2);
   echo("The xor of both map: \n");
   #using xor() method  
   var_dump($map1->xor($map2));
?7gt;

Output

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

The map1 elements are:
Ds\Map Object
(
    [0] => Hello
    [1] => Tutorials
    [2] => Point
    [3] => India
)
The map2 elements are:
Ds\Map Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)
The xor of both map:
object(Ds\Map)#3 (1) {
  [0]=>
  string(5) "Hello"
}

Example 3

If the values in both maps are exactly similar, the XOR() function returns an XOR of both maps as a new empty map ([]).

<?php  
   $map1 = new \Ds\Map(['a', 'e', 'i', 'o', 'u']);  
   $map2 = new \Ds\Map(['a', 'e', 'i', 'o', 'u']);
   echo "The map1 elements are: \n";
   print_r($map1);
   echo "The map2 elements are: \n";
   print_r($map2);
   echo("The xor of both map: \n");
   #using xor() method  
   var_dump($map1->xor($map2));
?>

Output

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

The map1 elements are:
Ds\Map Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The map2 elements are:
Ds\Map Object
(
    [0] => a
    [1] => e
    [2] => i
    [3] => o
    [4] => u
)
The xor of both map:
object(Ds\Map)#3 (0) {
}
Advertisements