0

I have two php functions, one returns a JSON array and the other attempts to decode it and access it's contents in order to strip a URL out of it. My problem is that I get the following errors..

1) json_decode() expects parameter 1 to be string

2) Invalid argument supplied for foreach() on line 77

Here is my code

php file 1

<?php

function getAnimation($userid, $db) {

    include('connect.php');

    $db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error() . "please contact [email protected] for technical assistance";
        echo "<br>";
    }

    $box_num = 1;

    $select = "SELECT card_id, order_num FROM decks WHERE box_num=$box_num AND id=$userid ORDER BY order_num";
    $result = mysqli_query($db, $select) or die("SQL Error 1: " . mysqli_error($db));

    while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
        $users[] = array(
            'card_id' => $row['card_id'],
            'order_num' => $row['order_num'],
        );
    }

    json_encode($users);

    return $users;

    mysqli_close($db);
}

?>

snippet of php file 2 that is attempting to decode the JSON array and access its contents.

include('getAnimation.php');
$animation = getAnimation($result_array[0], $db); //<< atempting to access returned array here
$obj = json_decode($animation);  //FIRST ERROR HERE
foreach ($obj->card_id as $item) { // SECOND ERROR HERE
$url = ($item->card_id);
echo $url; //show me the money
}

thanks for any help you can give!

0

3 Answers 3

2

Modify the return to be return json_encode($users). You're not actually setting the variable $users... you could also set it with $users = json_encode($users), then return $users.

To answer your second question, modify your code as such:

foreach($obj as $item) {
    $url = $item->card_id;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks :) I don;t quite understand whats going on here. COuld you elaborate a bit for me.
Sure- after you create your array with your function and json_decode it, you'll have an array of objects that you then just want to walk through (foreach) and access their properties (card_id, in this case)!
Just as an added thought, if i wanted to store the contents of the JSON array in an array, would I have to do something like.. foreach ($obj as $item) { $url[$i] = $item->card_id; $i++; echo $url[$i]; }
Doesn't look like $i would point to anything in that case... you'd have to do foreach ($obj as $i => $item) { } if you wanted to access the keys.
1

Change

json_encode($users);
return $users;

to:

$users = json_encode($users);
return $users;

That's it! :-)

Comments

1

json_decode() expects parameter 1 to be string

json_encode() returns a value, it doesn't act on the parameter by reference, so when you have:

json_encode($users);

return $users;

you're returning the original array, and not the JSON representation.

Fix it by doing:

return json_encode($users);

Invalid argument supplied for foreach() on line 77

You encode an array, so you should be decoding an array - so you should be looping over the array, not an object:

foreach ($obj->card_id as $item)

should just be:

foreach ($obj as $item) {
    $url = $item["card_id"];
    echo $url;
}

Side note

Avoid using generic variables names like $obj and $item - they are not descriptive, and PHP can have some unusual scoping quirks, especially if you are using buffering.

5 Comments

THanks, that fixed the first error, but I still get the second error. Any ideas?
sry - didn't read past the first error, as I thought the second was caused by the first. have addressed that now
Thanks for that answer it was very good, but I had already given best answer. Just as an added thought, if i wanted to store the contents of the JSON array in an array, would I have to do something like.. foreach ($obj as $item) { $url[$i] = $item->card_id; $i++; echo $url[$i]; }
well, once you've decoded the JSON string into a variable, it will be an array...
Oh right :) I thought you had to assign it to a php array to access its members. I want to pass each url to a javascript function in the same page. Thanks for your help. :)

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.