當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP Ds\Map merge()用法及代碼示例

Ds \ Map::merge()函數是PHP中的內置函數,用於返回添加所有給定關聯的結果。

用法:

Ds\Map public Ds\Map::merge( $values )

參數:此函數接受包含可遍曆對象或數組的單個參數$values。


返回值:此函數返回將給定的可遍曆對象或數組的所有鍵與其對應的值(與當前實例組合)相關聯。

以下示例程序旨在說明PHP中的Ds \ Map::merge()函數:

程序1:

<?php  
   
// Create new map 
$map = new \Ds\Map(["a" => 12, 
    "b" => 15, "c" => 18, "d" => 20]);  
   
// Merge the map element and display it  
print_r($map->merge(["a" => 1, "c" => 2, "f" => 3]));  
   
// Display the set element  
print_r($map)  
  
?> 
輸出:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 1
        )

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

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

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

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

)
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 12
        )

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

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

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

)

程序2:

<?php  
   
// Create new map 
$map = new \Ds\Map(["1" => "Geeks", 
        "2" => "for", "3" => "Geeks"]);  
   
// Merge the map element and display it  
print_r($map->merge(["a" => "Computer", 
        "b" => "Science", "c" => "Portal"]));  
  
?> 
輸出:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 1
            [value] => Geeks
        )

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

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

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

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

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

)

參考: https://www.php.net/manual/en/ds-map.merge.php



相關用法


注:本文由純淨天空篩選整理自R_Raj大神的英文原創作品 PHP | Ds\Map merge() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。