Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you decode a returned JSON array in php function
    primarykey
    data
    text
    <p>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..</p> <p>1) json_decode() expects parameter 1 to be string</p> <p>2) Invalid argument supplied for foreach() on line 77</p> <p>Here is my code</p> <p>php file 1</p> <pre><code>&lt;?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 d.g.folksman@ljmu.ac.uk for technical assistance"; echo "&lt;br&gt;"; } $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' =&gt; $row['card_id'], 'order_num' =&gt; $row['order_num'], ); } json_encode($users); return $users; mysqli_close($db); } ?&gt; </code></pre> <p>snippet of php file 2 that is attempting to decode the JSON array and access its contents.</p> <pre><code>include('getAnimation.php'); $animation = getAnimation($result_array[0], $db); //&lt;&lt; atempting to access returned array here $obj = json_decode($animation); //FIRST ERROR HERE foreach ($obj-&gt;card_id as $item) { // SECOND ERROR HERE $url = ($item-&gt;card_id); echo $url; //show me the money } </code></pre> <p>thanks for any help you can give!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload