简体   繁体   中英

Notice: Trying to get property of non-object -php

I'm trying to access json decode data's array elements. I'm getting an error while trying to access. Below is my php code with output decoded json data.

<?php
.
.
.

$json = $response;
$json_output = json_decode($json, true);
echo '<pre>';
print_r($json_output);
?>

Array
(
    [0] => Array
        (
            [alph_id] => 02adb5b4-d2fe-4a46-8798-1e2b876b2055
            [name] => Michael
            [s_id] => 1462339266273
            [avg_at] => 12.060000419617
            [distance] => 0
            [sw] => OFF
            [set_no] => 1
            [final_at] => 18.700000762939

        )

    [1] => Array
        (
            [alph_id] => 02adb5b4-d2fe-4a46-8798-1e2b876b2055
            [name] => John
            [s_id] => 1462339266273
            [avg_at] => 12.060000413454
            [distance] => 0
            [sw] => OFF
            [set_no] => 1
            [final_at] => 19.700000762939
        )
        .
        .
        .

I tried to access alph_id element, but its shows an error.

echo $json_output[0]->alph_id;  
Notice: Trying to get property of non-object

I think i correctly access the array element. What is wrong with that?

Thanks...

You make the json decode for associative array by using the true as second parameter.

So you need to access it as $json_output[0]['alph_id'] .

If you make like this,

$json_output = json_decode($json);

This json_decode returns the result as Object, then it should be,

$json_output[0]->alph_id; 

You used true in the second parameter of json_decode

json_decode($json, true);

when you define this should access to indexes with following structure

$json_output[0]['alph_id']; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM