-1

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.

user3176519
  • 393
  • 1
  • 9
  • 16

3 Answers3

0

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));
}
num8er
  • 18,604
  • 3
  • 43
  • 57
0

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
}
Community
  • 1
  • 1
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
0
$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';
}
voodoo417
  • 11,861
  • 3
  • 36
  • 40