Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Map::hasKey() Function



The PHP Ds\Map::hasKey() function in PHP is used to determine whether the map contains a given key. The key parameter can be any type (mixed).

This function returns a boolean value 'true', if the specified key is present in the current map, otherwise, it returns 'false'.

Syntax

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

public Ds\Map::hasKey(mixed $key): bool

Parameters

This function accepts a single parameter named 'key', which is described below −

  • key − The key needs to be checked.

Return value

This function returns 'true' if the key is found, or 'false' otherwise.

Example 1

The following program demonstrates the usage of the PHP Ds\Map::hasKey() function −

<?php
   $map = new \Ds\Map(["1" => 10, "2" => 20, "3" => 30]);
   echo "The map elements are: \n";
   print_r($map);
   $key = 2;
   echo "The key is: ".$key;
   echo "\nIs the key ".$key." is present in this map? ";
   #using hasKey() function
   var_dump($map->hasKey($key));
?>

Output

Once the above program is executed, it will display the following output −

The map elements are:
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
        )

)
The key is: 2
Is the key 2 is present in this map? bool(true)

Example 2

If the key is not found, this function returns 'false'.

Following is another example of the PHP Ds\Map::hasKey() function. We use this function to check whether the key "Tutorix" is present in this map (["Tutorials" => "1", "Point" => "2", "India" => "3"]) −

<?php 
   $map = new \Ds\Map(["Tutorials" => "1", "Point" => "2", "India" => "3"]);
   echo "The map elements are: \n";
   foreach($map as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }
   $key = "Tutorix";
   echo "The key is: ".$key;
   echo "\nIs the key '".$key."' is present in this map? ";
   var_dump($map->hasKey($key)); 
?>

Output

The above program produces the following output −

The map elements are:
[Tutorials] = 1
[Point] = 2
[India] = 3
The key is: Tutorix
Is the key 'Tutorix' is present in this map? bool(false)

Example 3

Using the hasKey() function result in conditional statement to check whether the specified key is present in this map ([0 => 'a', 1 => 'b', 2 => 'c']) −

<?php 
   $map = new \Ds\Map([0 => 'a', 1 => 'b', 2 => 'c']);
   echo "The map elements are: \n";
   foreach($map as $key=>$value){
	   echo "[".$key."] = ".$value."\n";
   }
   $key = 1;
   echo "The key is: ".$key."\n";
   $bool = $map->hasKey($key);
   if($bool){
	   echo "key is found";
   }
   else{
	   echo "Key does not found";
   }	
?>

Output

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

The map elements are:
[0] = a
[1] = b
[2] = c
The key is: 1
key is found
php_function_reference.htm
Advertisements