PHP array_key_exists() Function
In this chapter you will learn:
- Definition for PHP array_key_exists() Function
- Syntax for PHP array_key_exists() Function
- Parameter for PHP array_key_exists() Function
- Return value from PHP array_key_exists() Function
- Example - Check if the key "j" exists in an array
- Example - Check if the integer key "0" exists in an array
Definition
The array_key_exists() function checks existence of a specified key in an array.
Syntax
PHP array_key_exists() Function has the following syntax.
array_key_exists(key,array)
Parameter
Parameter | Is Required | Description |
---|---|---|
key | Required. | Key to look for |
array | Required. | Array to look for |
Return
PHP array_key_exists() Function returns true if the key exists and false if the key does not exist.
Example 1
Check if the key "j" exists in an array:
<?php/* j a v a 2 s .co m*/
$a=array("a"=>"A","b"=>"Bed","c"=>"Cat","j"=>"java2s.com");
if (array_key_exists("j",$a)){
echo "Key exists!";
}else{
echo "Key does not exist!";
}
?>
The code above generates the following result.
Example 2
Check if the integer key "0" exists in an array:
<?php/*jav a 2 s .c o m*/
$a=array("PHP","Java");
if (array_key_exists(0,$a)){
echo "Key exists!";
}else{
echo "Key does not exist!";
}
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_keys() function
- Syntax for PHP array_keys() function
- Parameter for PHP array_keys() function
- Example - Get all keys in an array
- Example - Get all keys for an associate array
- Example - Using the strict parameter, false
- Example - Using the strict parameter, true
Home » PHP Tutorial » PHP Array Functions