PHP - Ds Map::intersect() 函式



PHP 的 Ds\Map::intersect() 函式用於透過與另一個對映交叉鍵來建立一個新的對映。術語“交叉鍵”指的是兩個對映中的公共鍵。

新建立的對映包含當前例項的鍵值對,其鍵存在於另一個對映中。換句話說,它返回當前例項的副本,其中刪除了在另一個對映中不存在的所有鍵。

語法

以下是 PHP Ds\Map::intersect() 函式的語法:

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

引數

以下是此函式的引數:

  • map − 包含要與其交叉的鍵的另一個對映。

返回值

此函式返回當前例項和另一個對映的鍵交集。

示例 1

以下是 PHP Ds\Map::intersect() 函式的基本示例:

<?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));
?>

輸出

以上程式產生以下輸出:

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
        )

)

示例 2

如果給定對映中沒有公共鍵,則兩個對映的交集將為 empty()

以下是 PHP Ds\Map::intersect() 函式的另一個示例。我們使用此函式透過與該對映 (["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);
?>  

輸出

執行上述程式後,它將顯示以下輸出:

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
(
)

示例 3

在下面的示例中,我們使用此 Ds\Map::intersect() 函式透過與另一個對映交叉鍵來建立一個新的對映。

<?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);
?>

輸出

執行上述程式後,將顯示以下輸出:

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
        )

)
php_function_reference.htm
廣告