A bit shorter with array_diff
$musthave = array('a','b');
$test1 = array('a','b','c');
$test2 = array('a','c');
$containsAllNeeded = 0 == count(array_diff($musthave, $test1));
// this is TRUE
$containsAllNeeded = 0 == count(array_diff($musthave, $test2));
// this is FALSE
I think you're looking for the intersect function
array array_intersect ( array $array1 , array $array2 [, array $ ... ] )
array_intersect()
returns an array containing all values of array1 that are
present in all the arguments. Note that keys are preserved.
How about this:
function array_keys_exist($searchForKeys = array(), $searchableArray) {
$searchableArrayKeys = array_keys($searchableArray);
return count(array_intersect($searchForKeys, $searchableArrayKeys)) == count($searchForKeys);
}
The previous answers are all doing more work than they need to. Just use array_diff. This is the simplest way to do it:
$containsAllValues = !array_diff($search_this, $all);
That's all you have to do.
Source: Stackoverflow.com