How to check if an array key exists in PHP?

Sometimes you have array key and you want to get value and want to do operation. In that situations, there are few ways you get array value based on key.

isset() method

The fastest way to get key value of array by using isset() method. The isset() method will return false even if array value is null.


$arr = [null, true];

# false
# true

array_key_exists() method

array_key_exists(key, $array) returns true if the key from the $array exists, else it returns false


$search_array = array('first' => 1, 'second' => null);

array_key_exists('first', $search_array)
# true
array_key_exists('second', $search_array)
# true

array_key_exists() checks if the key exists, even if the value is null.