PHP array_key_exists() function to test whether a given key or index exists in an array or not. The array_key_exists() function returns TRUE on success or FALSE on failure.
PHP array_key_exists
PHP array_key_exists() is a built-in function that checks the array for a particular key, returns a true value if the key exists, and returns a false one if the key does not exist. If you skip the key when you define an array, the integer key is generated like an index, starting from 0 and increasing by 1 for each value.
Syntax
array_key_exists(key, array)
Arguments
The key is the value of the key to be checked.
An array is an input array in which the key will be checked.
Example
See the following code example.
<?php $arr = array("eleven" => 11, "dustin" => 21, "will" => 30, "nancy" => 40); if (array_key_exists("dustin", $arr)) { echo "Key exists!"; } else { echo "Key does not exist!"; }
See the output.
➜ pro php app.php Key exists! ➜ pro
Check indexed array keys
To check indexed array keys in PHP, use the array_key_exists() function. Let’s check if the integer key “0” exists in an array.
<?php $arr = array("eleven", "dustin", "will", "nancy"); if (array_key_exists(2, $arr)) { echo "Key exists!"; } else { echo "Key does not exist!"; }
See the output.
➜ pro php app.php Key exists! ➜ pro
Compare Performance array_key_exists with isset()
If you want to take the performance advantage of isset() while keeping the NULL element correctly detected.
if (isset(..) || array_key_exists(...)) { ... }
Benchmark (100000 runs):
array_key_exists(): 205 ms
is_set(): 35ms
isset() || array_key_exists(): 48ms
The above note on this page states that the isset() is significantly faster than array_key_exists(). It may be true except for one small hitch.
The isset() will return false for array keys that have their value set to NULL, which is therefore not entirely accurate.
See the following code.
<?php $eddie = array(); $eddie['broke'] = NULL; var_dump(isset($eddie['broke'])); var_dump(array_key_exists('broke', $eddie));
See the output.
➜ pro php app.php bool(false) bool(true) ➜ pro
Passing an empty array
Passing an empty array to the array_key_exists() function won’t return anything because no key exists.
See the following code.
<?php $arr = array(); if (array_key_exists(0, $arr)) { echo "Key exists!"; } else { echo "Key does not exist!"; }
See the output.
➜ pro php app.php Key does not exist! ➜ pro
If an array passed to the array_key_exists() function is NULL, the return value will also be NULL.
The way array_key_exists() function handles null, float, bool, and ‘integer-representing string’ keys is inconsistent in the case of boolean and float, with the way these are converted when used as array offset.
How To Check If An Array Element Exists
You can use either the language construct isset or the function array_key_exists. The isset should be a bit faster (as it’s not a function) but will return false if an item exists and has a NULL value.
<?php $st = array( 'eleven' => 'Millie', 'mike' => 'Finn', 'gaten' => NULL ); var_dump(isset($st['gaten'])); var_dump(array_key_exists('gaten', $st));
See the output.
➜ pro php app.php bool(false) bool(true) ➜ pro
That’s it for this tutorial.