PHP | Ds\Map __construct()函数
Ds\Map::__construct()函数是PHP中的一个内置函数,用于创建新实例。
句法:
public Ds\Map::__construct( $values )
参数:此函数接受单个参数$values ,该参数保存可遍历的对象或数组以使用初始值。
下面的程序说明了PHP中的Ds\Map::__construct()函数:
方案一:
"Geeks",
"2" => "for",
"3" => "Geeks"
]);
// Display the map elements
print_r($map);
?>
输出:
Ds\Map Object
(
)
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
)
)
方案二:
"10",
"2" => "20", "3" => 30]);
// Display key-value pair
print_r($map);
// Creating another Map
$map = new \Ds\Map([1 => "Welcome",
2 => "to", 3 => "GeeksforGeeks"]);
// Display key-value pair
print_r($map);
?>
输出:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 1
[value] => 10
)
[1] => Ds\Pair Object
(
[key] => 2
[value] => 20
)
[2] => Ds\Pair Object
(
[key] => 3
[value] => 30
)
)
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 1
[value] => Welcome
)
[1] => Ds\Pair Object
(
[key] => 2
[value] => to
)
[2] => Ds\Pair Object
(
[key] => 3
[value] => GeeksforGeeks
)
)
参考: https://www. PHP.net/manual/en/ds-map.construct。 PHP