PHP array_key_exists()| How To Check If An Array Element Exists
PHP array_key_exists() is an inbuilt function that checks the array for a particular key and returns a true value if the key exists and returns false 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 increases by 1 for each value.
PHP array_key_exists() Function
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.
See the following syntax.
array_key_exists(key, array)
The key is the value of the key to be checked.
An array is an input array, in which the key will be checked.
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
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
You’ll see that above note on this page stating 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 arrays 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
Pass Empty Array
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 itself and, 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 the value NULL.
<?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
Finally, PHP array_key_exists() Function | How To Check If An Array Element Exists over.
Recommended Posts
PHP array_intersect_key() Function Example
PHP array_fill_keys() Function