How to determine if an array contains a specific value in PHP? Last Updated : 06 Jun, 2024 Comments Improve Suggest changes Like Article Like Report Determining if an array contains a specific value in PHP involves verifying whether a particular element exists within an array. This task is essential for various programming scenarios, such as searching for user input, validating data, or filtering results based on specific criteria. PHP offers several built-in functions to facilitate this process, ensuring efficient and reliable array manipulation. The below programs illustrate how to determine if an array contains a specific value: Table of Content Using in_array() FunctionUsing array_key_exists() functionUsing in_array() FunctionThe in_array() function in PHP checks if a specific value exists in an array. It returns `true` if the value is found and `false` otherwise. This function is case-sensitive and can be used to search for both scalar values and objects within an array. Example: php <?php //array containing elements $names = array('Geeks', 'for', 'Geeks'); //search element $item = 'Geeks'; if(in_array($item, $names)){ //the item is there echo "Your element founded"; } else{ //the item isn't there echo "Element is not found"; } ?> Output Your element foundedUsing array_key_exists() functionThe array_key_exists() function checks if a specific key exists in an array. It takes two parameters: the key to check and the array to search in. If the key exists, it returns true; otherwise, it returns false. This approach is suitable for associative arrays. Example: PHP <?php // Nikunj Sonigara $arr = array('gfg', 1, 17); // Check if value 'gfg' exists if (array_key_exists(0, $arr)) { echo "'gfg' exists in the array\n"; } else { echo "'gfg' does not exist in the array\n"; } // Check if value 18 exists if (array_key_exists(18, $arr)) { echo "18 exists in the array\n"; } else { echo "18 does not exist in the array\n"; } ?> Output'gfg' exists in the array 18 does not exist in the array Comment More infoAdvertise with us Next Article How to determine if an array contains a specific value in PHP? S ShivangiMeena Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to Check a Value Exist at Certain Index in an Array in PHP? Given an array and index, the task is to check whether a value exists or not at a certain index in an array in PHP. Below are the approaches to check if a value exists at a certain index or not in an array in PHP:Table of ContentUsing isset() FunctionUsing array_key_exists() FunctionUsing key_exists 3 min read How to Check if a Value Exists in an Associative Array in PHP? 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 2 min read How to Check an Array Contains only Unique Values in PHP? In PHP, verifying that an array contains unique values is a common task. This involves ensuring that each element in the array appears only once. There are multiple methods to achieve this check effectively:Table of ContentUsing array_unique () and count() functionUsing array_count_values() function 4 min read How to print all the values of an array in PHP ? We have given an array containing some array elements and the task is to print all the values of an array arr in PHP. In order to do this task, we have the following approaches in PHP:Table of ContentUsing for-each loop: Using count() function and for loop: Using implode():Using print_r() FunctionUs 3 min read How to search by multiple key => value in PHP array ? In a multidimensional array, if there is no unique pair of key => value (more than one pair of key => value) exists then in that case if we search the element by a single key => value pair then it can return more than one item. Therefore we can implement the search with more than one key = 5 min read Like