PHP - Ds Map::intersect() Function
The PHP Ds\Map::intersect() function is used to create a new map by intersecting keys with another map. The term "intersecting keys" refers to the common keys in both maps.
The newly created map contains the key-value pairs from the current instance whose keys are present in another map. In other words, it returns a copy of the current instance with all keys removed that are not present in the other map.
Syntax
Following is the syntax of the PHP Ds\Map::intersect() function −
public Ds\Map::intersect(Ds\Map $map): Ds\Map
Parameters
Following is the parameter of this function −
- map − The other map contains the keys to intersect with.
Return value
This function returns a key intersection of the current instance and another map.
Example 1
The following is the basic example of the PHP Ds\Map::intersect() function −
<?php $map1 = new \Ds\Map(["1" => 10, "2" => 20, "4" => 40]); echo "The map1 elements are: \n"; print_r($map1); $map2 = new \Ds\Map(["2" => 20, "4" => 40, "5" => 50]); echo "The map2 elements are: \n"; print_r($map2); echo "The intersection of map1 and map2 is: \n"; #using intersect() function print_r($map1->intersect($map2)); ?>
Output
The above program produces 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] => 4
[value] => 40
)
)
The map2 elements are:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 2
[value] => 20
)
[1] => Ds\Pair Object
(
[key] => 4
[value] => 40
)
[2] => Ds\Pair Object
(
[key] => 5
[value] => 50
)
)
The intersection of map1 and map2 is:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 2
[value] => 20
)
[1] => Ds\Pair Object
(
[key] => 4
[value] => 40
)
)
Example 2
If no keys are common in the given maps, the intersection of both maps will be empty ().
Following is another example of the PHP Ds\Map::intersect() function. We use this function to create a new map by intersecting keys with this map (["4" => "Point", "5" => "India"]) −
<?php
$map1 = new \Ds\Map(["1" => "Tutorials", "2" => "Point", "3" => "India"]);
echo "The map1 elements are: \n";
foreach($map1 as $key=>$value){
echo "[".$key."] = ".$value."\n";
}
$map2 = new \Ds\Map(["4" => "Point", "5" => "India"]);
echo "\nThe map2 elements are: \n";
foreach($map2 as $key=>$value){
echo "[".$key."] = ".$value."\n";
}
echo "The intersection of map1 and map2 is: \n";
#using intersect() function
$new_map = $map1->intersect($map2);
print_r($new_map);
?>
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: [4] = Point [5] = India The intersection of map1 and map2 is: Ds\Map Object ( )
Example 3
In the example below, we use this Ds\Map::intersect() function to create a new map by intersecting keys with another map.
<?php
$map1 = new \Ds\Map([1 => 'a', 2 => 'e', 3 => 'i']);
echo "The map1 elements are: \n";
foreach($map1 as $key=>$value){
echo "[".$key."] = ".$value."\n";
}
$map2 = new \Ds\Map([3 => 'o', 4 => 'u']);
echo "\nThe map2 elements are: \n";
foreach($map2 as $key=>$value){
echo "[".$key."] = ".$value."\n";
}
echo "The intersection of map1 and map2 is: \n";
#using intersect() function
$new_map = $map1->intersect($map2);
print_r($new_map);
?>
Output
Once the above program is executed, the following output will be displayed:
The map1 elements are:
[1] = a
[2] = e
[3] = i
The map2 elements are:
[3] = o
[4] = u
The intersection of map1 and map2 is:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 3
[value] => i
)
)