How do you check if an index exists in php?

Is there a way to check if an array index exists or is null? isset() doesn't tell you whether the index doesn't exist or exists but is null. If I do : isset($array[$index]) || is_null($array[$index]) it won't work because if the index doesn't exist is_null will crash.

How can I check this please? Also is there a way to check only if something exist, no matter if it is set to null or not?

How do you check if an index exists in php?

asked Mar 9, 2013 at 11:43

4

The function array_key_exists() can do that, and property_exists() for objects, plus what Vineet1982 said. Thanks for your help.

How do you check if an index exists in php?

akinuri

9,70010 gold badges58 silver badges94 bronze badges

answered Mar 9, 2013 at 12:04

Virus721Virus721

7,7799 gold badges60 silver badges118 bronze badges

4

This is the very good question and you can use get_defined_vars() for this:

$foo = NULL;
$a = get_defined_vars();

if (array_key_exists('def', $a)) {
   // Should evaluate to FALSE
 }; 

if (array_key_exists('foo', $a)) {
   // Should evaluate to TRUE
};

This will solve your problem

answered Mar 9, 2013 at 12:02

Vineet1982Vineet1982

7,5824 gold badges29 silver badges65 bronze badges

1

(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)

array_key_existsChecks if the given key or index exists in the array

Description

array_key_exists(string|int $key, array $array): bool

Parameters

key

Value to check.

array

An array with keys to check.

Return Values

Returns true on success or false on failure.

Note:

array_key_exists() will search for the keys in the first dimension only. Nested keys in multidimensional arrays will not be found.

Examples

Example #1 array_key_exists() example

$search_array = array('first' => 1'second' => 4);
if (
array_key_exists('first'$search_array)) {
    echo 
"The 'first' element is in the array";
}
?>

Example #2 array_key_exists() vs isset()

isset() does not return true for array keys that correspond to a null value, while array_key_exists() does.

$search_array = array('first' => null'second' => 4);// returns false
isset($search_array['first']);// returns true
array_key_exists('first'$search_array);
?>

Notes

Note:

For backward compatibility reasons, array_key_exists() will also return true if key is a property defined within an object given as array. This behaviour is deprecated as of PHP 7.4.0, and removed as of PHP 8.0.0.

To check whether a property exists in an object, property_exists() should be used.

See Also

  • isset() - Determine if a variable is declared and is different than null
  • array_keys() - Return all the keys or a subset of the keys of an array
  • in_array() - Checks if a value exists in an array
  • property_exists() - Checks if the object or class has a property

manhon824 at gmail dot com

11 years ago

I took hours for me to debug, and I finally recognized that,

You have to reset the $array before using array_key_exists
reset($array);
array_key_exists($needle,$array);

Or you will get no reply.

How do I check if an index exists in an array?

To check if an array index exists, access the array at the specific index and check if the result is not equal to undefined . If the result is not equal to undefined the array index exists.

How do I find a key in an array?

The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

How do I find associative array in PHP?

How to check if PHP array is associative or sequential? There is no inbuilt method in PHP to know the type of array. If the sequential array contains n elements then their index lies between 0 to (n-1). So find the array key value and check if it exist in 0 to (n-1) then it is sequential otherwise associative array.

What is an array key PHP?

The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns null .