How to check an element is exists in array or not in PHP ? Last Updated : 31 Oct, 2021 Comments Improve Suggest changes Like Article Like Report 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 is used to check the presence of an element in the array. The method returns true or false depending on whether the element exists in the array or not. in_array(element , array) Arguments : element: The element to check in the arrayarray: The array to look for element Example: PHP <?php // Declaring an array object $arr = array("Hello", "GEEKs" , "User" , "PHP"); print("Original Array </br>"); print (json_encode($arr) . " </br>"); // Declaring element $ele = "GEEKs"; // Check if element exists if(in_array($ele, $arr)){ print($ele . " - Element found."); }else{ print($ele . "Element not found."); } ?> Output: Original Array: ["Hello","GEEKs","User","PHP"] GEEKs - Element found. Approach 2 (Using for loop): A for loop iteration is done throughout the array. A boolean flag is declared to check for the presence of an element. It is initialized to the boolean value of false. In case the value is false, and the element is found in the array, the flag value is changed to the true value. No further modifications in the flag value are made. Example: PHP <?php // Declaring an array object $arr = array(1, 3, 5, 6); print("Original Array </br>"); print (json_encode($arr)." </br>"); // Declaring element $ele = 5; // Declare a flag $flag = FALSE; // Check if element exists foreach($arr as $val){ if($flag == FALSE){ if($val == $ele){ $flag = TRUE; } } } if($flag ==TRUE){ print($ele . " - Element found."); } else{ print($ele . " - Element not found."); } ?> Output: Original Array [1, 3, 5, 6] 5 - Element found. Comment More infoAdvertise with us Next Article How to check an element is exists in array or not in PHP ? Y yashkumar0457 Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-Questions Similar Reads How to Check if an Element Exists in an Array in JavaScript? Given an array, the task is to check whether an element present in an array or not in JavaScript. If the element present in array, then it returns true, otherwise returns false.The indexOf() method returns the index of first occurance of element in an array, and -1 of the element not found in array. 2 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 Check whether Element Exists in Java ArrayList? Java ArrayList is a resizable array, which can be found in java.util package. We can add or delete elements from an ArrayList whenever we want, unlike a built-in array. We can check whether an element exists in ArrayList in java in two ways: Using contains() methodUsing indexOf() method Method 1: Us 2 min read 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 Like