PHP - Ds Map::get() 函式



PHP 的 Ds\Map::get() 函式用於檢索當前對映例項中給定鍵的值。

如果指定的鍵不存在於對映中,則此函式將丟擲“OutOfBoundsException”(如果未提供預設值),或者如果提供了可選的預設值,則返回該預設值。

語法

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

public Ds\Map::get(mixed $key, mixed $default = ?): mixed

引數

以下是此函式的引數:

  • key - 要在對映中查詢的鍵。
  • default - 當未找到鍵時返回的可選預設值。

返回值

此函式返回給定鍵的值,或者如果找不到鍵,則返回可選的預設值。

示例 1

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

<?php  
   $map = new \Ds\Map([1, 2, 3, 4, 5]);
   echo "The map elements are: \n";
   print_r($map);
   $key = 1;
   echo "\nThe key is: ".$key;
   echo "\nThe element associates with key ".$key." is: ";
   var_dump($map->get(1)); 
?>

輸出

以上程式產生以下輸出:

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

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

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

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

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

)

The key is: 1
The element associates with key 1 is: int(2)

示例 2

如果指定的鍵在當前對映中未找到並且未提供可選的預設值,則 PHP Ds\Map::get() 函式將丟擲“OutOfBoundsException”。

<?php  
   $map = new \Ds\Map(["1" => "Tutorials", "2" => "Point", "3" => "India"]);
   echo "The map elements are: \n";
   print_r($map);
   $key = 5;
   echo "\nThe key is: ".$key;
   echo "\nThe element associates with key ".$key." is: ";
   var_dump($map->get($key)); 
?> 

輸出

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

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 key is: 5
The element associates with key 5 is: PHP Fatal error:  
Uncaught OutOfBoundsException: Key not found in C:\Apache24\htdocs\index.php:8
Stack trace:
#0 C:\Apache24\htdocs\index.php(8): Ds\Map->get(5)
#1 {main}
  thrown in C:\Apache24\htdocs\index.php on line 8

示例 3

如果在當前例項中未找到給定的鍵,則get() 函式將在輸出中返回提供的default 值:

<?php  
   $map = new \Ds\Map(["1" => 'a', "2" => 'e', "3" => 'i', "4" => 'o', "5" => 'u']);
   echo "The map elements are: \n";
   print_r($map);
   $key = 10;
   echo "\nThe key is: ".$key;
   $default = 'Z';
   echo "\nThe default value is: ".$default;
   echo "\nThe element associates with key ".$key." is: ";
   var_dump($map->get($key, $default)); 
?>

輸出

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

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

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

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

    [3] => Ds\Pair Object
        (
            [key] => 4
            [value] => o
        )

    [4] => Ds\Pair Object
        (
            [key] => 5
            [value] => u
        )

)

The key is: 10
The default value is: Z
The element associates with key 10 is: string(1) "Z"
php_function_reference.htm
廣告