简体   繁体   中英

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.

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"}] )

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?

I´m not sure about the value of $query , but if it is something like:

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? It seems like what you want is to recursively search $query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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