PHP - Ds Map::keys() 函式



PHP 的 Ds\Map::keys() 函式用於檢索當前對映的鍵集。返回的鍵集中的鍵的順序與它們在對映例項中新增的順序完全相同。

語法

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

public Ds\Map::keys(): Ds\Set

引數

此函式不接受任何引數。

返回值

此函式返回一個包含對映所有鍵的集合。

示例 1

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

<?php 
   $map = new \Ds\Map([1 => "Tutorials", 2 => "Point", 3 => "India"]);
   echo "The map elements are: \n";
   print_r($map);
   echo "The set of map keys: \n";
   #using keys() function
   print_r($map->keys());
?>

輸出

上述程式產生以下輸出:

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

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

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

)
The set of map keys:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)

示例 2

以下是 PHP Ds\Map::keys() 函式的另一個示例。我們使用此函式來檢索此對映(["first" => "Tutorials", "second" => "Point", "third" => "India"])的鍵集:

<?php 
   $map = new \Ds\Map(["first" => "Tutorials", "second" => "Point", "third" => "India"]);
   echo "The map elements are: \n";
   foreach($map as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }
   echo "\nThe set of map keys: \n";
   #using keys() function
   print_r($map->keys());
?>

輸出

執行上述程式後,它將生成以下輸出:

The map elements are:
[first] = Tutorials
[second] = Point
[third] = India
The set of map keys:
Ds\Set Object
(
    [0] => first
    [1] => second
    [2] => third
)
php_function_reference.htm
廣告