Q. How do I check if a specific value exists in an array in PHP. Write a sample PHP program to check if a value exists in an array.
Using PHP in_array() function
Use PHP in_array()
function to check whether a specific value exists in an array or not.
Advertisement
Here is an sample PHP program, initialized an array with few element. Then check if an element is available in the defined array.
Example
1 2 3 4 5 6 7 | <?php $colors = array("Yellow", "Green", "Blue", "Pink", "Black"); if(in_array("Green", $colors)){ echo "Green element found in colors array"; } ?> |
Output:
Green element found in colors array