Reputation: 1995
Example:
$array = array('hi', 'hello', 'bye');
How can I check if at least one of two or more values are present in the array ?
like:
if(in_array('hi', $array) || in_array('hello', $array)) ...
but with a single check? can it be done?
Upvotes: 0
Views: 114
Reputation: 1361
use array_intersect()... i.e.
$array = array('hi', 'hello', 'bye');
if(count(array_intersect($array, array('search', 'for', 'values')))>0) ....
Upvotes: 1
Reputation: 2434
Take a look at preg_grep, which will return entries in an array that match a predefined pattern (e.g. '/^(hi|hello)$/
in your example).
E.g.
if (count(preg_grep('^/(hi|hello)$/',$array)))
{
// your code
}
Upvotes: 1
Reputation: 101604
function in_array2($ary1,$ary2){
return count(array_intersect($ary1,$ary2)) > 0;
}
Simple, make use of array_intersect.
Upvotes: 1
Reputation: 17434
if(count(array_intersect(array('hi','hello','bye'), $array))) {
...
}
Upvotes: 6