0

假设我有随机 $variable 和三个数组:

$array1 = ("one", "two", "three");
$array2 = ("four", "five", "six");
$array3 = ("seven", "eight", "nine");

这个怎么做:

if $variable is in $array1
echo "in array one";
elseif $variable is $array2
echo "in array two";
elseif $variable is in $array3
echo "in array three";
else
echo "variable isnt in any of arrays";

我希望你明白这一点,我真的很感激任何帮助,我正在考虑一个一个地搜索数组中的项目,然后 preg_matching 它们。但我不知道如何用多个数组来做到这一点:)

4

2 回答 2

1

您可以使用该in_array()功能。[文档]

您的代码如下所示:

if (in_array ($variable, $array1)) {
 echo "in array 1";
} else if(in_array ($variable, $array2)) {
 echo "in array 2";
} [...] else {
 echo "isnt in any";
}
于 2012-10-23T17:29:47.613 回答
0

您需要使用,请参阅此处in_array的手册

  if (in_array($variable, $array1))
   echo "in array one";
  else if (in_array($variable, $array2))
    echo "in array two";
  else if (in_array($variable, $array3))
    echo "in array three";
  else
    echo "variable isnt in any of arrays";
于 2012-10-23T17:28:59.907 回答