How to Check if a Value Exists in an Associative Array in PHP? Last Updated : 02 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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 ContentUsing in_array() FunctionUsing array_search() FunctionUsing a LoopApproach 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. PHP <?php $student_marks = array( "Maths" => 95, "Physics" => 90, "Chemistry" => 96, "English" => 93, "Computer" => 98 ); // Check a value exists in the array if (in_array(90, $student_marks)) { echo "Value exists in the array"; } else { echo "Value does not exist in the array"; } ?> OutputValue exists in the arrayApproach 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. PHP <?php $student_marks = array( "Maths" => 95, "Physics" => 90, "Chemistry" => 96, "English" => 93, "Computer" => 98 ); // Check a value exists in the array $key = array_search(90, $student_marks); if ($key !== false) { echo "Value exists in the array"; } else { echo "Value does not exist in the array"; } ?> OutputValue exists in the arrayApproach 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: PHP <?php $student_marks = array( "Maths" => 95, "Physics" => 90, "Chemistry" => 96, "English" => 93, "Computer" => 98 ); $value_to_check = 90; $value_exists = false; foreach ($student_marks as $subject => $mark) { if ($mark == $value_to_check) { $value_exists = true; break; } } if ($value_exists) { echo "Value exists in the array"; } else { echo "Value does not exist in the array"; } ?> OutputValue exists in the array Comment More infoAdvertise with us Next Article How to Check if a Value Exists in an Associative Array in PHP? B blalverma92 Follow Improve Article Tags : PHP PHP-array Similar Reads How to check if a value exists in an Array in Ruby? In this article, we will discuss how to check if a value exists in an array in ruby. We can check if a value exists in an array through different methods ranging from using Array#member? method and Array#include? method to Array#any? method Table of Content Check if a value exists in an array using 3 min read How to check if an Array contains a value or not? There are many ways for checking whether the array contains any specific value or not, one of them is: Examples: Input: arr[] = {10, 30, 15, 17, 39, 13}, key = 17Output: True Input: arr[] = {3, 2, 1, 7, 10, 13}, key = 20Output: False Approach: Using in-built functions: In C language there is no in-b 3 min read How to check an element is exists in array or not in PHP ? An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP 2 min read How to check if File Exists in PHP ? To check whether any file is existing or not then we can use the below-mentioned PHP function. To find the existence of the files, we use file_exists() function. This function is used to check whether a file or directory exists or not. Syntax: file_exists( $path ) Parameters: This function accept on 1 min read How to check if a String contains a Specific Character in PHP ? In PHP, determining whether a string contains a specific character is a common task. Whether you're validating user input, parsing data, or performing text processing, PHP provides several methods to check for the presence of a particular character within a string efficiently. Here are some common a 2 min read How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar 6 min read How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte 3 min read How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra 2 min read How to get specific key value from array in PHP ? In this article, we will see how to get specific key values from the given array. PHP array is a collection of items that are stored under keys. There are two possible types of keys: strings and integers. For any type of key, there is a common syntax to get a specific value by key â square brackets. 3 min read How to Check Whether a Variable is Empty in PHP? Given some values of variables, the task is to check whether the variable is empty or not in PHP. An empty variable can refer to a variable that has not been set, a variable that has been explicitly set to an empty value, or a variable that contains a value that is considered empty. In this article, 5 min read Like