PHP Ds Map diff()函数用法解读

Ds \ Map :: diff()function是PHP中的内置函数, 用于使用包含第一张地图中未包含在另一张地图中的元素的键创建地图。
语法如下:

Ds\Map public Ds\Map::diff( $map )

参数:该函数接受单个参数$地图用于保存需要排除其值的地图元素。
返回值:它会返回一个新地图, 其中包含第一个地图中其他地图中不存在的元素。
下面的程序说明了Ds \ Map :: diff()PHP中的功能:
程序1:
< ?php // PHP program to illustrate the diff() // function of Ds\map // Creating a Map $map1 = new \Ds\Map([ "1" => "10" , "3" => 30, "4" => 40]); // Creating another Map $map2 = new \Ds\Map([ "2" => "20" , "3" => 35, "5" => 50, "6" => 60]); echo "Difference between two map: < br> " ; print_r( $map1 -> diff( $map2 )); ?>

输出如下:
Difference between two map: Ds\Map Object ( [0] => Ds\Pair Object ( [key] => 1 [value] => 10 )[1] => Ds\Pair Object ( [key] => 4 [value] => 40 ))

【PHP Ds Map diff()函数用法解读】程式2:
< ?php // PHP program to illustrate the diff() // function of Ds\map // Creating a Map $map1 = new \Ds\Map([ "1" => "Geeks" , "2" => "for" , "3" => "Geeks" ]); // Creating another Map $map2 = new \Ds\Map([ "2" => "for" , "3" => "Geeks" , "4" => "lsbin" ]); echo "Difference between two map: < br> " ; print_r( $map1 -> diff( $map2 )); ?>

输出如下:
Difference between two map: Ds\Map Object ( [0] => Ds\Pair Object ( [key] => 1 [value] => Geeks ))

参考: https://www.php.net/manual/en/ds-map.diff.php

    推荐阅读