【发布时间】:2012-01-31 05:17:50
【问题描述】:
我有一个从数据库表中取出的变量,如下所示:
while($r = mysql_fetch_array($query) //The query is just: SELECT * FROM stuff_table WHERE userid='$userid'
{
$stuff = $r['stuff'];
}
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...";
}
我从中得到的输出是:“只有汽车”而不是“汽车和书籍”。
那么在继续执行 if/elseif 语句之前,如何让我的代码检查数组中的 2 个或更多值? in_array 函数可以吗?
变量转储:
array(4) { [0]=> string(4) "cars" [1]=> string(6) " books" [2]=> string(10) " computers" [3]=> string(6) " foods" }
【问题讨论】:
-
您的代码看起来还不错。有什么问题?
-
能否请您提供
var_dump($array_of_stuff)的输出? -
@MДΓΓ БДLL 我希望它检查数组中的 2 个值,但它没有。我不知道为什么,但它不明白该数组有 2 个我要在其中寻找的值。
-
将 var_dump($array_of_stuff) 的输出添加到您的问题中,解决方案可能会立即出现。
-
你需要修剪前导空格!
标签: php mysql arrays variables