Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I'm not sure if it's possible to do it with in_array. What I need is to verify that all given values exist in array. For example:

$a = array(1,2,3,4,5,6,7,8,9,10)
$b = array(1,2,3);

if(in_array($b, $a)) {
   return true
} else {
  return false
}

Note that all the values from $b must exist in $a in order to return true.

share|improve this question

marked as duplicate by blubb, andrewsi, Divi, Rakib, Machavity Jun 6 '14 at 3:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Maybe walk through $b in foreach and than test with in_array? –  panther Jun 5 '14 at 18:55
    
try array_intersect or array_diff –  Deadooshka Jun 5 '14 at 18:56

3 Answers 3

up vote 0 down vote accepted
$a = array(1,2,3,4,5,6,7,8,9,10);
$b = array(1,2,3);

if(!array_diff($b, $a)) {
   echo '$b is subset of $a';
} else {
  echo '$b isn`t subset of $a';
}
share|improve this answer

Duplicate of Here.

Use array_diff()

$arr1 = array(1,2,3);
$arr2 = array(1,2,3,4,5,6,7);
$arr3 = array_diff($arr1, $arr2);
if (count($arr3) == 0) {
  // all of $arr1 is in $arr2
}
share|improve this answer

try this:

function arrayExists($needle, $haystack) {
    return sizeof(array_intersect($needle, $haystack)) == sizeof($needle);
}

also can use this if your needle array has duplicate values:

function arrayExists($needle, $haystack) {
    return sizeof(array_intersect(array_unique($needle), array_unique($haystack))) == sizeof(array_unique($needle));
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.