Find if an array has at least 1 of the values
arrays, php
Solution
You might want to use array_intersect()
$search_values = array('cat', 'horse', 'dog');
$results = array('cat', 'horse');
if ( count ( array_intersect($search_values, $results) ) > 0 ) {
echo 'BINGO';
} else {
echo 'NO MATCHES';
}
Problem
I need to find out, in PHP, if an array has any of the values of the other array. For example : ``` $search_values = array('cat', 'horse', 'dog'); $results = array('cat', 'horse'); if (in_array($search_values, $results)) echo 'A value was found'; ``` Of course, the above does not really work (in_array). Basically, based on the above example, I want to check if in the $results array, there is either a cat, hourse or a dog. Do I need to do a "foreach" in the 1st array, then do an "in_array" in the 2sd one, and return true; if it is found? Or is there a better way?