PHP mySQL 的条件 in_array 问题
我有 firePHP,所以我确切地知道变量是什么,但我不明白为什么这段代码没有改变它。
我从 mySQL 调用 $query 收到(如果返回则生成 [{"type":"2"}]
) 我有 4 种潜在类型,并且事物可以是多种类型(即 [{"type":"1"},{"type":"2"}]
)
现在我想读取此数据并根据它的类型运行其他各种函数,即:如果它只是类型2,则调用函数TWO,如果它是类型1和2,则调用函数ONE和函数TWO。我认为如果我将所有数据移动到另一个数组中,这将是最简单的。
这是我当前拥有的代码:
$result = array('message'=>false, 'money'=>false, 'glasses'=>false, 'exclamation'=>false);
if (in_array('1',$query)) {$result['message'] = true;}
if (in_array('2',$query)) {$result['money'] = true;}
if (in_array('3',$query)) {$result['glasses']=true;}
if (in_array('4',$query)) {$result['exclamation']=true;}
echo json_encode($result);
但是,这不会更新 $result 数组(因为我可以告诉 firePHP 中 $message
的所有值都是 false
...因此我认为我的 if 语句有问题,但是什么呢?
I have firePHP so i know exactly what the variables are, but I can't figure out why this code doesn't change it.
I receive from a mySQL call $query (which if returned produces [{"type":"2"}]
)
I have 4 potential types, and things can be multiple types (i.e. [{"type":"1"},{"type":"2"}]
)
Now I want to read this data and run various other functions based on the type it has, that is: if it's only type 2, call function TWO, if it's type 1 and 2 call function ONE and function TWO. I thought this would be easiest if i moved all the data into another array.
Here is the code I currently have:
$result = array('message'=>false, 'money'=>false, 'glasses'=>false, 'exclamation'=>false);
if (in_array('1',$query)) {$result['message'] = true;}
if (in_array('2',$query)) {$result['money'] = true;}
if (in_array('3',$query)) {$result['glasses']=true;}
if (in_array('4',$query)) {$result['exclamation']=true;}
echo json_encode($result);
This however does not update the $result array (as I can tell all of the values of $message
are false
in firePHP.... Thus I assume something is wrong with my if statements, but what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定
$query
的值,但如果它是这样的:您将必须使用:
因为这是变量的值。
I´m not sure about the value of
$query
, but if it is something like:You would have to use:
as that is the value of your variable.
是不是因为 $query 返回的结果是数组的数组,因此 in_array 只在顶层搜索而不是子层搜索?看来您想要的是递归搜索 $query。
Is it because the results returned in $query are arrays of arrays, and thus in_array is only searching at the top level and not sub-levels? It seems like what you want is to recursively search $query.