1

I have the following code

$var = "cat";
$array = ["cat", "dog", "mouse"];

How would I check if any item from $array is in $var? Something like

if($array in $var) {
    task()
}

1 Answers1

0

in_array is the way to go! Like this:

if(in_array($var, $array)){
    task();
}

in_array($needle, $haystack) searches your Array $haystack for your var $needle. returns true if your Array contains your var.

jophab
  • 5,356
  • 14
  • 41
  • 60
Philip G
  • 4,098
  • 2
  • 22
  • 41