PHP array_key_exists() Function
PHP Tutorial » PHP array key exists () Function
The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.
Syntax:
Parameters:
key -> Specifies the key
array -> Specifies an array
array_key_exists() - example
array_key_exists() will search for the keys in the first dimension only. in the next example check if the key "yellow" exists in an array:
<!DOCTYPE html>
<html>
<body><?php
$a=array("red"=>"Ferrari","yellow"=>"Lamborghini", "blue"=>"Ford");
if (array_key_exists("yellow",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?></body>
</html>
Key exists!
PHP array key exists () More Examples
Check if the key "black" exists in an array:
<!DOCTYPE html>
<html>
<body><?php
$a=array("red"=>"Ferrari","yellow"=>"Lamborghini", "blue"=>"Ford");
if (array_key_exists("black",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?></body>
</html>
Key does not exist!
What's the difference between isset() and array_key_exists() - example
array_key_exists will tell you if a key exists in an array return true or false, otherwise isset () will only return true if the key or variable exists in array and is not null.
$a = array('key1' => 'red', 'key2' => 'blue', 'key3' => null);
isset($a['key2']); // true
array_key_exists('key2', $a); // true
isset($a['key3']); // false
array_key_exists('key3', $a); // true
There is another important difference: isset doesn't complain when $a does not exist, while array_key_exists does.
PHP array key exists() Function, vs isset, multiple keys, and not empty, check, nested, in array, undefined index, case insensitive, PHP array_key_exists vs isset
PHP array key exists() Function, vs isset, multiple keys, and not empty, check, nested, in array, undefined index, case insensitive, PHP array_key_exists vs isset
PHP array_key_exists() Function - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.