0

I'm trying to parse a JSON string. Before parsing I put my data in JSON format, but I am getting a "trying to get property of non-object" error.

Here is my code:

$querys = "SELECT * FROM image_refrences WHERE building_id=''";
$rows = array();
$responce= array();
$data = $conn->query($querys);

while($row = $data->fetch_assoc()){
    $json['images'][] = array('id' => $row['id'],
                              'url' => $row['image_file'],
                              'location' => $row['location'] );     
}

$responce= json_encode($json, TRUE);
$rows=json_decode($responce,TRUE);

foreach ( $rows->images as $output ) {
    echo $output->id;
}

My JSON string will look like this:

{"images":[
    {"id":"1","url":"def6a9.jpg","location":""},
    {"id":"2","url":"def6a9.jpg","location":""},
    {"id":"3","url":"fullsize_distr.jpg","location":""}
]}

Can someone help me find what I'm doing wrong?

7
  • json['images'] is not valid PHP syntax, variables need to start with $. Commented Jul 18, 2016 at 19:36
  • The second argument to json_encode() is supposed to be an integer containing flags. Why are you using TRUE? Commented Jul 18, 2016 at 19:38
  • Why are you encoding your array as json just to decode it again in the next row? Commented Jul 18, 2016 at 19:39
  • @Barmar issue is same for $json['images'] as well Commented Jul 18, 2016 at 19:44
  • I know it is. That looks like just a copying error, which you should fix. Commented Jul 18, 2016 at 19:46

1 Answer 1

3

Remove the second argument from this: $rows=json_decode($responce,TRUE);

With that true, you are decoding to a multidimensional array rather than an array of objects, and you are trying to access it using object syntax with $rows->images and $output->id.


Or instead, if you want to keep decoding it as an array, then keep the true argument and use array syntax to access the result:

foreach ( $rows['images'] as $output ) {
    echo $output['id'];
}

After making the changes, your code should be like this:

while($row = $data->fetch_assoc()){
    $json['images'][] = array('id' => $row['id'], 'url' => $row['image_file'], 'location' => $row['location'] );
}

$responce = json_encode($json);           // Remove TRUE
$rows = json_decode($responce);           // Remove TRUE

foreach ( $rows->images as $output ) {
    echo $output->id;
}

I may be making too many assumptions. I assume you are just experimenting with the json functions, because you are encoding to JSON and then immediately decoding. If you actually don't need JSON, you can skip all of that and just output the ids in your while loop.

while ($row = $data->fetch_assoc()){
    echo $row['id'];
}
Sign up to request clarification or add additional context in comments.

10 Comments

I've removed that and changed next line with this one $rows=json_decode($json,TRUE); but it is still not working :(
Have you removed the TRUE from both the json_encode and the json_decode? Neither should have it for the rest of your code to work properly.
@TashenJazbi - Are you defining your $json-variable before you start using it?
@MagnusEriksson I'm assuming $json is defined in the preceding while loop, and that the missing dollar sign there is just a typo in the question.
@Don'tPanic issue is still same with dollar sign too... now updated code says " json_decode() expects parameter 1 to be string, array given" and in foreach loop it says "Invalid argument supplied for foreach()"
|

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.