2012-10-23 77 views
0

匹配變量可以說我有隨機$變量和三個數組:幾個陣列

$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他們,但我無法弄清楚如何使用多個陣列:)

回答

0

您需要使用in_array,請參閱ma NUAL here

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"; 
+0

謝謝配合;)真的很感激 – DadaB

1

可以使用in_array()功能。 [Doc]

你的代碼是這樣:

if (in_array ($variable, $array1)) { 
echo "in array 1"; 
} else if(in_array ($variable, $array2)) { 
echo "in array 2"; 
} [...] else { 
echo "isnt in any"; 
} 
+0

大聲笑,謝謝:d:d愚蠢的我didint即使約in_array – DadaB

+0

到現在也沒這個功能之前, ((; – tobspr