We have given an array arr and a Key key, the task is to check if a key exists in an array or not in PHP.
Input : arr = ["Geek1", "Geek2", "1", "2","3"] key = "2" Output : Found the Key Input : arr = ["Geek1", "Geek2", "1", "2","3"] key = 9 Output : Key not Found
The problem can be solved using PHP inbuilt function for checking key exists in a given array. The in-built function used for the given problem are:
Method 1: Using array_key_exists() Method: The array_key_exists() function checks whether a specific key or index is present inside an array or not.
boolean array_key_exists( $index, $array )
Found the Key
Method 2: Using isset() Method: The isset() function checks whether a specific key or index is present inside an array or not.
bool isset( mixed $var, mixed $... )
Found the Key
- How to check whether an array is empty using PHP?
- How to check an array is multidimensional or not in PHP ?
- How to check an array is associative or sequential in PHP?
- Mongoose | exists() Function
- Why <big> tag is not in HTML5 while <small> tag exists ?
- How to add http:// if it doesn't exists in the URL in PHP?
- Node.js | fs.exists() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.