PHP array key exists

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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

Pictorial Presentation :

php-array-array_key_exists()

Example :

  1. $array1=array("Orange" => 100, "Apple" => 200, "Banana" => 300, "Cherry" => 400);  

  2. if (array_key_exists("Banana",$array1))  

  3. {  

  4. echo "Array Key exists...";  

  5. }  

  6. else  

  7. {  

  8. echo "Array Key does not exist...";  

  9. }  

  10. ?>  

Output :

Array Key exists.