How do you check if a key exists in an array php?
(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8) Show 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
❮ PHP Array Reference ExampleCheck if the key "Volvo" exists in an array: $a=array("Volvo"=>"XC90","BMW"=>"X5"); Try it Yourself » Definition and UsageThe 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. Tip: Remember that if you skip the key when you specify an array, an integer key is generated, starting at 0 and increases by 1 for each value. (See example below) Syntaxarray_key_exists(key, array) Parameter Values
Technical Details
More ExamplesExampleCheck if the key "Toyota" exists in an array: $a=array("Volvo"=>"XC90","BMW"=>"X5"); Try it Yourself » ExampleCheck if the integer key "0" exists in an array: $a=array("Volvo","BMW"); Try it Yourself » ❮ PHP Array Reference How do you find the key in an array?You can use array_keys() to get ALL the keys of an array, e.g.
What is array_keys () used for in PHP?The array_keys() function returns an array containing the keys.
How do you check if an index is present in an array?indexOf() The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
How do you check if a key exists in an array JavaScript?Using the indexOf() Method
JavaScript's indexOf() method will return the index of the first instance of an element in the array. If the element does not exist then, -1 is returned.
|