我有一個變量,我得到了這樣的數據庫表:如何檢查多個值的數組?
while($r = mysql_fetch_array($query) //The query is just: SELECT * FROM stuff_table WHERE userid='$userid'
{
$stuff = $r['stuff'];
}
的東西變量看起來是這樣的:「汽車,書籍,電腦,食品」和我做了一個數組:
//I used:
$array_of_stuff = explode(",", $stuff); //This gave me an array.
現在我要檢查它的值,如:
if(in_array("cars", $array_of_stuff) && in_array("books", $array_of_stuff))
{
//This line is the problem, I want it to check to see if it has "cars" and "books" in the array but this code is not working for that like it should. Instead of checking and finding both, it just goes on to the next elseif.
echo "Cars and Books";
} elseif(in_array("cars")) {
//
echo "Only cars";
} elseif(in_array("books")) {
echo "Only books";
} else {
echo "Other stuff...";
}
我從這個得到的輸出是:不是「汽車與書」「只有汽車」。
那麼我怎樣才能讓我的代碼檢查數組的2個或更多的值,然後繼續其if/elseif語句? in_array函數可能嗎?
瓦爾轉儲:
array(4) { [0]=> string(4) "cars" [1]=> string(6) " books" [2]=> string(10) " computers" [3]=> string(6) " foods" }
你的代碼看起來很好。有什麼問題? – 2011-12-31 21:45:56
你能給我一個'var_dump($ array_of_stuff)'的輸出嗎? – Lars 2011-12-31 21:46:47
@MДΓΓБДLL我希望它檢查2個值的數組,但它不。我不知道爲什麼,但它不明白該數組有2個值,我正在尋找它。 – randomphp 2011-12-31 21:48:16