PHP - Ds Map::hasValue() Function



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

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

Syntax

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

public Ds\Map::hasValue(mixed $value): bool

Parameters

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

  • value − The value needs to be checked.

Return value

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

Example 1

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

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

Output

The above program produces 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 value is: 20
Is the value 20 is present in this map? bool(true)

Example 2

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

Following is another example of the PHP Ds\Map::hasValue() function. We use this function to check whether the value "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";
   }
   $value = "Tutorix";
   echo "The value is: ".$value;
   echo "\nIs the value '".$value."' is present in this map? ";
   var_dump($map->hasValue($value)); 
?>

Output

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

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

Example 3

Using the Ds\Map::hasValue() function result within the conditional statement to check whether the specified value 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";
   }
   $value = 1;
   echo "The value is: ".$value."\n";
   $bool = $map->hasValue($value);
   if($bool){
	   echo "Value is found";
   }
   else{
	   echo "Value does not found";
   }	
?>

Output

Once the above program is executed, it generates the following output −

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