当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。