0

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?

1
  • 1
    Can you please put the complete output of $query, using print_r for instance ? Commented May 23, 2011 at 15:15

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh, wow I knew it was going to be something simple...I had tried in_array('2', $query[0]) but that gave an error, why is that different?
@mazlix It's different because 2 is only a part of the value held in array(0 => '{"type":"2"}') The only way your code would have worked would have been if the array looked like this array(0 => 2)
@mazlix Your value is not an array in php, it is a string. It´s the object notation for javascript, but php only sees a string.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.