![]() |
Given an Associative array, the task is to check whether a value exists in the associative array or not. There are two methods to check if a value exists in an associative array, these are – using in_array(), and array_search() functions. Let’s explore each method with detailed explanations and code examples. Table of Content Approach 1: Using in_array() FunctionThe in_array() function checks if a value exists in an array. However, this function is not suitable for associative arrays, as it only works for indexed arrays.
Output Value exists in the array Approach 2: Using array_search() FunctionThe array_search() function searches an array for a given value and returns the corresponding key if the value is found. If the value is not found, it returns false.
Output Value exists in the array Approach 3: Using a LoopUsing a loop to iterate through the array and check if a value exists can be more flexible and provides better control over the search process. This method is especially useful when you want to perform additional operations during the search or handle more complex conditions. Example:
Output Value exists in the array |
Reffered: https://www.geeksforgeeks.org
PHP |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |