如何在php中编写if(或switch)语句来一次检查数组的所有值


How to write an if(or switch) statement to check all the values of an array in one go in php

是否可以在php中一次检查if语句中数组的所有值?

。我有数组:

$testing = array(true, true, true);

我想做一些像

if($testing == (true, true, true)){
   //do something
}else if ($testing == (true, true, false)){
   //do something else
}
etc...

如果这是不可能的,谁有什么想法可以做到这一点?很多谢谢!

yes, in_array() function检查给定数组中的字符串

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
   echo "Got Irix";
}
if (in_array("mac", $os)) {
 echo "Got mac"; 
}
?>

你可以这样比较它们:

$testing = Array(true, true, true);
if($testing == array(true, true, true)){
   //do something
}else if ($testing == array(true, true, false)){
   //do something else
}

您不需要再次使用相同的变量$testing

你的意思是这样吗?

$a = array('a','b','d');
if ($a == array('a','b','c')) {
    // first
} elseif ($a == array('a','b','d')) {
    // second
}

Try array_diff:

$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);

我认为你需要一个这样的脚本:

$testing = array(true, true, true);
foreach ($testing as $key = > $value)
{
   if ($value != TRUE)
      {
       die();
      }
   else
      {
       // do something
      }
}