VOOZH about

URL: https://www.geeksforgeeks.org/php/how-to-check-if-a-value-exists-in-an-associative-array-in-php/

⇱ How to Check if a Value Exists in an Associative Array in PHP? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Check if a Value Exists in an Associative Array in PHP?

Last Updated : 23 Jul, 2025

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.

Approach 1: Using in_array() Function

The 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() Function

The 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 Loop

Using 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


Comment
Article Tags:
Article Tags: