PHP - Ds Map::putAll() Function
The PHP Ds\Map::putAll() function is used to associate all key-value pairs from a given traversable object or array into an existing Ds\Map. This function allows you to insert multiple key-value pairs at once, making it more efficient than inserting each pair individually using the put() method.
Syntax
Following is the syntax of the PHP Ds\Map::putAll() function −
public Ds\Map::putAll(mixed $pairs): void
Parameters
Following is the parameter of this function −
- pairs − A traversal object or array.
Return value
This function doesn't return any value.
Example 1
The following program demonstrates the usage of the PHP Ds\Map::putAll() function −
<?php $map = new \Ds\Map(); echo "The original map elements are: \n"; print_r($map); echo "The map after putAll() function called: \n"; #using putAll() function $map->putAll([1 => 10, 2 => 20, 3 => 30, 4 => 40, 5 => 50]); print_r($map); ?>
Output
After executing the above program, it will display the following output −
The original map elements are:
Ds\Map Object
(
)
The map after putAll() function called:
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
)
[3] => Ds\Pair Object
(
[key] => 4
[value] => 40
)
[4] => Ds\Pair Object
(
[key] => 5
[value] => 50
)
)
Example 2
Following is another example of the PHP Ds\Map::putAll() function. We use this function to associate all the key-value pairs with this given traversal object or array −
<?php $map = new \Ds\Map(["a" => "Tutorials"]); echo "The original map elements are: \n"; print_r($map); echo "The map after putAll() function called: \n"; #using putAll() function $map->putAll(["b" => "Point", "c" => "India"]); print_r($map); ?>
Output
The above program generates the following output −
C:\Apache24\htdocs>php index.php
The original map elements are:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => a
[value] => Tutorials
)
)
The map after putAll() function called:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => a
[value] => Tutorials
)
[1] => Ds\Pair Object
(
[key] => b
[value] => Point
)
[2] => Ds\Pair Object
(
[key] => c
[value] => India
)
)