1

So i have this json I got from a previous query:

{"HUID":"1","UIDS":"1,2,3","name":"home","type":"page","cat_id":"home"}

I print it like:

$SJON_String = json_encode($returnArray[0]);
echo $SJON_String;

Works, nice formatting.

Now I extract the UIDS string:

$JSON_Decoded = json_decode($SJON_String);
$JSON_UIDS = $JSON_Decoded->{'UIDS'}; 

echo "1st JSON UIDS: ".$JSON_UIDS."<BR>";

It prints nice string:

1,2,3

Now I want to use this string to give to my next query:

foreach ($JSON_UIDS as $UID_get)
    {

     echo "Now: ".$UID_get. "<BR>";

     $query2 = "SELECT * 
                FROM items
                WHERE UID LIKE '%" . $UID_get. "%' 
                ORDER BY name";

     if($result2 = $server->query($query2))
     {
       while ($row2 = $result2->fetch_assoc())
       {
        array_push($returnArray2, $row2);
        }
     }      

It will give me an error: Warning: Invalid argument supplied for foreach(), because it's not a valid string object.

If I replace $JSON_UIDS with JSON_Decoded it will work, but that's not what I want.

I tried formatting the string in a bunch of different ways but I can't get it to accept the extracted string. I tried declaring as string() before, after, formatting it as "1,2,3", with quotes, going a little insane..

There must be a good and efficient way...

0

2 Answers 2

3

You're trying to loop through a string. Try turning the string into an array:

$UIDS = explode( ',', $JSON_UIDS );

foreach( $UIDS as $UID_get ) {
    ...
Sign up to request clarification or add additional context in comments.

Comments

1

You're trying to loop through "1,2,3" - You need to create some type of list or an array. Possibly using explode()

$UIDS = explode(',', $JSON_UIDS);

You can access them now at $UIDS[0], $UIDS[1], $UIDS[2]

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.