简体   繁体   English

PHP mySQL有条件的in_array问题

[英]conditional in_array trouble with PHP mySQL

I have firePHP so i know exactly what the variables are, but I can't figure out why this code doesn't change it. 我有firePHP,所以我确切地知道变量是什么,但是我不知道为什么这段代码没有改变它。

I receive from a mySQL call $query (which if returned produces [{"type":"2"}] ) I have 4 potential types, and things can be multiple types (ie [{"type":"1"},{"type":"2"}] ) 我从mySQL调用$ query收到消息(如果返回,则产生[{"type":"2"}] ),我有4种可能的类型,事物可以是多种类型(即[{"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. 现在,我想读取此数据并根据其类型运行各种其他功能,即:如果仅是类型2,则调用函数TWO,如果是类型1和2,则调用函数ONE和函数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? 但是,这不会更新$ result数组(因为我可以告诉我们在firePHP中$message所有值都是false的。...因此,我假设我的if语句有问题,但是怎么办?

I´m not sure about the value of $query , but if it is something like: 我不确定$query的值,但是如果是这样的话:

array [0 => '{"type":"2"}']

You would have to use: 您将必须使用:

in_array('{"type":"2"}',$query)

as that is the value of your variable. 因为那是您变量的值。

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? 是因为$ query返回的结果是数组数组,因此in_array仅在顶层而不是子层进行搜索? It seems like what you want is to recursively search $query. 似乎您想要的是递归搜索$ query。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM