1

I get this Error Notice: Trying to get property of non-object each time the array is at index 0.

But works if there are more than 1 value in the array.

$jsonurl = "https://example.org/json_response/";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json, true);
foreach ( $json_output as $output ) {   
  echo $output->id;
}

Using var_dump($json_output); does returns the data.

Edited

array (size=2)
  'id' => string 'xYue78ee9es' (length=10)
  'username' => string 'peesBEE' (length=7)

But throws Error when I try to accessing the variable like this echo $output->id;.

7
  • 1
    var_dump($output); to see the content of each iteration. Given the Notice message, the first one isn't even an object Commented Apr 29, 2015 at 13:30
  • Can you show me $json_output array ? Commented Apr 29, 2015 at 13:31
  • yes i can see the content of each iteration, as normal. It actually works if there are more than 1 iteration Commented Apr 29, 2015 at 13:31
  • since you already set the flag to true on json_decode, you're already free of any objects inside your variable, so using the -> arrow operator is unneeded Commented Apr 29, 2015 at 13:33
  • 1
    @user123451 as i have said earlier, once you've set that flag to true, what comes out of it is an array, treat it as such. posting your var_dump($json_output) would help those who answered below Commented Apr 29, 2015 at 13:41

3 Answers 3

5

As I have said already in the comments above, once you've set your json_decode flag as true, in turn, you'll get an array instead of an object.

Following in the comments:

Can you show me $json_output array ? – Prakash
@Prakash, array (size=1) 'id' => string 'xYue78ee9es' (length=10) – user123451

It seems its just a normal flat array, then treat it as such. Actually, you wouldn't need a foreach anymore. You can just access individual elements directly:

echo $json_output['id'];
echo $json_output['username'];

If you still would like to use a foreach, then it'll just traverse the first level, so you wouldn't need to put any index anymore:

foreach($json_output as $output) {
    echo $output;
    // no need for that ['id'] or ->id anymore
    // its just strings on $output
}

If you'd like to restrict some elements from getting echoed inside the foreach loop, simple if statement testing the keys should suffice:

foreach($json_output as $key => $output) {
    if($key === 'id') {
        echo $output; // echoes only id
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

yea, but what if i just want to restrict the output to just id alone. The code above displays both id and username, not specific on which data to output.
@user123451 are you referring to pointing to individual elements? just use echo $json_output['id'];
@user123451 if you'd want some filtering, just add an if inside the loop, check my revision
0

Check json before foreach

if ($json_output && !empty($json_output)) {
    foreach ( $json_output as $output ) {   
        echo $output->id;
    }
}

Comments

0

$json_output will be a php array, therefore you need to change

echo $output->id;

to

echo $output['id'];

Edit

Based on the var_dump you provided it seems that you need to remove the foreach loop and just use echo $output['id']

2 Comments

Ok, so now its giving me Error warning - " Warning: Illegal string offset 'id' "
do a var_dump($json_output) and post the results

Your Answer

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