PHP | Ds\Map union()函数
Ds\Map::union()函数是PHP中的一个内置函数,用于创建包含两个地图并集的新地图。
句法:
Ds\Map Ds\Map::union( $map )
参数:此函数接受单个参数$map ,该参数用于保存实例的另一个映射以与当前实例组合。
返回值:它返回一个包含两个映射并集的映射。
下面的程序说明了PHP中的 Ds\Map::union()函数:
方案一:
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
)
)
方案二:
"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