PHP - Ds Map::pairs() Function
The PHP Ds\Map::pairs() function is used to retrieve a sequence containing all pairs of the map. The term pairs refers to the keys and their respective values of the map elements.
The orders of pairs in the sequence will be the same as the entries that were added to the map.
Syntax
Following is the syntax of the PHP Ds\Map::pairs() function −
public Ds\Map::pairs(): Ds\Sequence
Parameters
This function does not accept any parameter.
Return value
This function returns a sequence containing all pairs of a map.
Example 1
The following program demonstrates the usage of the PHP Ds\Map::pairs() function −
<?php $map = new \Ds\Map([10, 20, 30]); echo "The original map elements are: \n"; print_r($map); echo "Sequence of all pairs: \n"; #using pairs() function print_r($map->pairs()); ?>
Output
The above program produces the following output −
The original map elements are:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 0
[value] => 10
)
[1] => Ds\Pair Object
(
[key] => 1
[value] => 20
)
[2] => Ds\Pair Object
(
[key] => 2
[value] => 30
)
)
Sequence of all pairs:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 0
[value] => 10
)
[1] => Ds\Pair Object
(
[key] => 1
[value] => 20
)
[2] => Ds\Pair Object
(
[key] => 2
[value] => 30
)
)
Example 2
Following is another example of the PHP Ds\Map::pairs() function. We use this function to retrieve a sequence of all pairs of this map ([1 => "Tutorials", 2 => "Point", 3 => "India"]) −
<?php $map = new \Ds\Map([1 => "Tutorials", 2 => "Point", 3 => "India"]); echo "The original map elements are: \n"; print_r($map); echo "The sequence of all pairs: \n"; print_r($map->pairs()); ?>
Output
After executing the above program, it will display the following output −
The original map elements are:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 1
[value] => Tutorials
)
[1] => Ds\Pair Object
(
[key] => 2
[value] => Point
)
[2] => Ds\Pair Object
(
[key] => 3
[value] => India
)
)
The sequence of all pairs:
Ds\Map Object
(
[0] => Ds\Pair Object
(
[key] => 1
[value] => Tutorials
)
[1] => Ds\Pair Object
(
[key] => 2
[value] => Point
)
[2] => Ds\Pair Object
(
[key] => 3
[value] => India
)
)