PHP | Ds\Map diff()函数
Ds\Map::diff()函数是PHP中的一个内置函数,用于使用包含第一个映射的元素的键创建一个映射,这些元素在另一个映射中不存在。
句法:
Ds\Map public Ds\Map::diff( $map )
参数:此函数接受单个参数$map ,该参数用于保存需要排除其值的地图元素。
返回值:它返回一个新地图,其中包含第一个地图中不存在于另一个地图中的元素。
下面的程序说明了PHP中的Ds\Map::diff()函数:
方案一:
"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:
";
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
)
)
方案二:
"Geeks",
"2" => "for",
"3" => "Geeks"]);
// Creating another Map
$map2 = new \Ds\Map([
"2" => "for",
"3" => "Geeks",
"4" => "GeeksforGeeks"]);
echo "Difference between two map:
";
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