-6

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

例如我有数组:

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

我想做类似的事情

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

如果不可能,是否有人对如何做到这一点有任何想法?非常感谢!

4

5 回答 5

1

是的,in_array() 函数检查给定数组中的字符串。

<?php
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
   echo "Got Irix";
}
if (in_array("mac", $os)) {
 echo "Got mac"; 
}
?>
于 2013-05-30T09:44:37.853 回答
0

你可以像这样比较它们:

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

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

您不需要$testing再次使用相同的变量。只需创建一个新数组进行比较。

于 2013-05-30T09:46:40.307 回答
0

你的意思是这样的?

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

} elseif ($a == array('a','b','d')) {
    // second
}
于 2013-05-30T09:48:27.023 回答
0

尝试 array_diff:

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

print_r($result);
于 2013-05-30T09:48:53.577 回答
0

我认为您需要这样的脚本:

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

foreach ($testing as $key = > $value)
{
   if ($value != TRUE)
      {
       die();
      }
   else
      {
       // do something
      }
}
于 2013-05-30T09:56:54.700 回答