PHP - Ds Map::keys() Function



The PHP Ds\Map::keys() function is used to retrieve the set of keys of the current map. The order of the keys in the returned set will be exactly the same as they were added in the map instance.

Syntax

Following is the syntax of the PHP Ds\Map::keys() function −

public Ds\Map::keys(): Ds\Set

Parameters

This function does not accept any parameter.

Return value

This function returns a set containing all keys of a map.

Example 1

The following is the basic example of the PHP Ds\Map::keys() function −

<?php 
   $map = new \Ds\Map([1 => "Tutorials", 2 => "Point", 3 => "India"]);
   echo "The map elements are: \n";
   print_r($map);
   echo "The set of map keys: \n";
   #using keys() function
   print_r($map->keys());
?>

Output

The above program produces the following output −

The 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 set of map keys:
Ds\Set Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)

Example 2

Following is another example of the PHP Ds\Map::keys() function. We use this function to retrieve a set of keys of this map (["first" => "Tutorials", "second" => "Point", "third" => "India"]) −

<?php 
   $map = new \Ds\Map(["first" => "Tutorials", "second" => "Point", "third" => "India"]);
   echo "The map elements are: \n";
   foreach($map as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }
   echo "\nThe set of map keys: \n";
   #using keys() function
   print_r($map->keys());
?>

Output

After executing the above program, it will generate the following output −

The map elements are:
[first] = Tutorials
[second] = Point
[third] = India
The set of map keys:
Ds\Set Object
(
    [0] => first
    [1] => second
    [2] => third
)
php_function_reference.htm
Advertisements