How to Check If a Value Exists in An Array in PHP

Channel: Linux
Abstract: Green element found in colors array$colors)){ echo "Green element found in colors array"

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.

Here is an sample PHP program, initialized an array with few element. Then check if an element is available in the defined array.

Example

<?php $colors = array("Yellow", "Green", "Blue", "Pink", "Black"); if(in_array("Green", $colors)){ echo "Green element found in colors array"; } ?>1234567<?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

Ref From: tecadmin
Channels: PHParray

Related articles