Php in_array search for more than 1 string

Hello forums

I have this array :

$a = array(1=>red,2=>blue,3=>green,4=>black);

I want to see if blue and red is in the array;

I tried:

if (in_array('red,blue',$a)){
  echo 'Yes';
}else{
   echo 'Nope';
}

But it’s returning false. I need it like: when red exists in the array and blue does’t it will return false, meaning red and blue must be in the array to return true. is there a proper way to do this?

Thanks

Ok I figured a dirty solution


$a = array(1=>'red',2=>'blue',3=>'green');
$s = array(1=>'red',2=>'blue');

$i=0;
$c = count($s);
foreach($s as $k=>$v){
  if(in_array($v,$a)){$i++;}
  if($i==$c){$i=0; return true;}else( return false;); 
	
}

Eeek… anyone has a better one? Please share

Thanks

Maybe something like this?


<?php
function values_in_array(array $needles, array $haystack){
  return count($needles) === count(array_intersect($needles, $haystack));
}

$haystack = array('yellow', 'red', 'green', 'blue');

var_dump(
  values_in_array(array('red', 'purple'), $haystack)
); # bool(false)

var_dump(
  values_in_array(array('red', 'blue'), $haystack)
); # bool(true)

Wow array_intersect is much cleaner than looping
You rule!

Thanks,