HOW to check values are present in an Array?

There are various scenarios in which you want to check whether the given value is present in array or not.

So, here’s the simple PHP function which we can use to check.

$search = 'orange';

$fruits = array( 'banana', 'orange', 'apple' );

if ( in_array( $search, $fruits ) ) {
	echo "Available";
} else {
	echo "Not Available";
}

Output of above code will be Available

Now, what if we want to check whether multiple values present in an array or not.

$search = array('orange', 'banana');

$fruits = array( 'banana', 'orange', 'apple' );

If we want to check whether all fruits from $search array is present in $fruits, then we need to run following code

if ( count( array_intersect( $search, $fruits ) ) == count( $fruits ) ) {
	echo "All Fruits Are Available";
} else {
	echo "All Fruits Are Not Available";
}

But, if want to check any of the fruits are available in an array, then

if ( count( array_intersect( $search, $fruits ) ) > 0 ) {
	echo "Available";
} else {
	echo "Not Available";
}

Hope, this will be useful

Scroll to Top