PHP Check if an Array Key Exists

  302 views   6 months ago 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.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]