How to determine if an array contains a specific value in PHP?
Given an array and a specific value, we have to determine whether the array contains that value or not. So there is two popular way to determine that the specific value is in the array or not. One procedure is using by loop and other one is in_array() function. By using loop to get the specific value present or not is time-consuming compared to in_array() function so here we will skip the loop one and use the in_array() function.
Below programs illustrate how to determine if an array contains a specific value:
Program 1:
<?php //array containig 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 founded
Program 2: In this example the in_array function perform in strict mode, it will also check the type of array elements.
<?php $name = array("Python", "Java", "C#", 3); //Seraching python if (in_array("python", $name, TRUE)) { echo "found \n"; } else { echo "not found \n"; } //Seraching 3 if (in_array(3, $name, TRUE)) { echo "found \n"; } else { echo "not found \n"; } //Seraching Java if (in_array("Java", $name, TRUE)) { echo "found \n"; } else { echo "not found \n"; } ?> |
Output:
not found found found
Program 3:
<?php $arr= array('gfg', 1, 17); if (in_array('gfg', $arr)) { echo " gfg \n"; } if (in_array(18, $arr)) { echo "18 found with strict check\n"; } ?> |
Output:
gfg
In the above example, the array contains string “gfg” and two numbers 1 and 17. The if statement calls in_array() function which checks if the given item as the parameter is present in the given array or not. If the function returns true, the if statement is executed.
Recommended Posts:
- How to determine length or size of an Array in Java?
- How to insert an item into array at specific index in JavaScript?
- Determine the first and last iteration in a foreach loop in PHP?
- How to determine active route in AngularJS ?
- How to determine the content of HTML elements overflow or not ?
- How to determine the direction of a jQuery scroll event?
- How to determine which element the mouse pointer move over using JavaScript ?
- Domain Specific Tools
- How to position a div at specific coordinates ?
- How to find element with specific ID using jQuery ?
- How to adjust CSS for specific zoom level?
- How to get specific number of child elements using CSS?
- How to get text of specific option tag using jQuery?
- Removing occurrences of a specific character from end of a string in PHP
- How to insert a string at a specific index in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

