Is there any way to simplify two "in_array" php functions into one function?

I’m trying to simplify two “in_array” functions into one, if it doesn’t have it in php it’s not a problem, in theory it would weigh more on php to create a new way, it’s only if it already has it.

$array = array('foo','bar','gold');

if(in_array($array, 'foo') && in_array($array, 'bar')){
  echo 'work';
}

You can work with filter

If(count(array_filter(array, function($a)
{
     return $a == 'foo' or $a == 'bar';  
})) == 2)
…

Thank you, resolved.

I would go for array_diff:

$array = array('foo','bar','gold');

if (array_diff(['foo', 'bar'], $array) === []) {
  echo 'work';
}
1 Like

Thank you, resolved.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.