w3resource

PHP: array_key_exists() function

PHP: Checks if the given key or index exists in an array

The array_key_exists() function is used to check whether a specified key is present in an array or not.

The function returns TRUE if the given key is set in the array. The key can be any value possible for an array index.

Version:

(PHP 4 and above)

Syntax:

array_key_exists(array_key, array_name)

Parameters:

Name Description Required /
Optional
Type
array_key Value to check. Required Mixed*
array_name The specified array whose keys will be checked. Required Array

*Mixed: Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value

TRUE on success or FALSE on failure.

Value Type: Boolean

Example:

<?php
$array1=array("Orange" => 100, "Apple" => 200, "Banana" => 300, "Cherry" => 400);
if (array_key_exists("Banana",$array1))
{
echo "Array Key exists...";
}
else
{
echo "Array Key does not exist...";
}
?>

Output:

Array Key exists.

Pictorial Presentation:

php function reference: array_key_exists() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous:array_intersect
Next: array_keys



PHP: Tips of the Day

PHP: Extract numbers from a string

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);