PHP - Ds Map::putAll() 函式



PHP 的 Ds\Map::putAll() 函式用於將給定可遍歷物件或陣列中的所有鍵值對關聯到現有的 Ds\Map 中。此函式允許您一次插入多個鍵值對,使其比使用 put() 方法逐個插入每個對更有效。

語法

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

public Ds\Map::putAll(mixed $pairs): void

引數

以下是此函式的引數:

  • pairs − 可遍歷物件或陣列。

返回值

此函式不返回值。

示例 1

以下程式演示了 PHP Ds\Map::putAll() 函式的使用:

<?php  
   $map = new \Ds\Map();
   echo "The original map elements are: \n";
   print_r($map);
   echo "The map after putAll() function called: \n";
   #using putAll() function
   $map->putAll([1 => 10, 2 => 20, 3 => 30, 4 => 40, 5 => 50]);
   print_r($map);
?>

輸出

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

The original map elements are:
Ds\Map Object
(
)
The map after putAll() function called:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => 10
        )

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

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

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

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

)

示例 2

以下是 PHP Ds\Map::putAll() 函式的另一個示例。我們使用此函式將所有鍵值對與給定的可遍歷物件或陣列關聯:

<?php  
   $map = new \Ds\Map(["a" => "Tutorials"]);
   echo "The original map elements are: \n";
   print_r($map);
   echo "The map after putAll() function called: \n";
   #using putAll() function
   $map->putAll(["b" => "Point", "c" => "India"]);
   print_r($map);
?>

輸出

上述程式生成以下輸出:

C:\Apache24\htdocs>php index.php
The original map elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => Tutorials
        )

)
The map after putAll() function called:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => Tutorials
        )

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

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

)
php_function_reference.htm
廣告