How do you check if an index exists in php?
Is there a way to check if an array index exists or is null? Show 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?
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.
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:
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_exists — Checks if the given key or index exists in the array Descriptionarray_key_exists(string|int Parameterskey Value to check. array An array with keys to check. Return Values Returns
ExamplesExample #1 array_key_exists() example
Example #2 array_key_exists() vs isset() isset() does not return
Notes
See Also
manhon824 at gmail dot com ¶ 11 years ago
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 .
|