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


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


Ds \ Map::union()函數是PHP中的內置函數,用於創建包含兩個映射的並集的新映射。

用法:

Ds\Map Ds\Map::union( $map )

參數:該函數接受單個參數$map,該參數用於保存實例的其他映射以與當前實例結合。


返回值:它返回一個包含兩個映射的並集的映射。

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

程序1:

<?php  
  
// Declare a new map 
$a = new \Ds\Map(["a" => 1, "b" => 3, "c" => 5]);  
  
// Declare another new map 
$b = new \Ds\Map(["a" => 2, "c" => 3, "d" => 6]);  
  
// Print the Union of two map 
echo("Union of both map is:\n");  
  
print_r($a->union($b)); 
  
?>
輸出:
Union of both map is:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => 2
        )

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

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

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

)

程序2:

<?php  
  
// Declare a new map 
$a = new \Ds\Map(["a" => "Geeks", "b" => "for", "c" => "Geeks"]);  
  
// Declare another new map 
$b = new \Ds\Map(["b" => "Computer", "e" => "Science", "f" => "Portal"]);  
  
// Print the union of two map 
echo("Union of both map is:\n");  
  
print_r($a->union($b)); 
  
?>
輸出:
Union of both map is:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => a
            [value] => Geeks
        )

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

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

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

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

)

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



相關用法


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