1

I have one json data but i can't parsing because first type is not have name.

    [
{"id":325,"distance":239,"text":"test","position":{"lat":2,"lon":3}},
{"id":333,"distance":123,"text":"test","position":{"lat":2,"lon":3}},
{"id":331,"distance":1,"text":"test","position":{"lat":2,"lon":3}}
    ]

Php code

$jsonurl = "url address";
$json = file_get_contents($jsonurl);

$obj = json_decode($json,true);

$it = "<ul>"
    foreach ($obj[0] as $list) 
    {
        $it .= "<h4>".$list['id']."</h4>"
    }

1 Answer 1

1

Its stdClass Object not an array, so access it like this,

$it .= "<h4>".$list->id."</h4>";

And also change your for loop like this

foreach ($obj as $list) 
            ^ remove [0] here

Finally your loop looks like this,

foreach ($obj as $list) 
{
    $it .= "<h4>".$list->id."</h4>"
}

see demo here

Sign up to request clarification or add additional context in comments.

7 Comments

if you print_r($obj), what you are getting. Do this before for loop.
Array ( [0] => Array ( [id] => 325 [distance] => 239 [text] => test [position] => Array ( [lat] => 2 [lon] => 3 ) ) [1] => Array ( [id] => 333 [distance] => 123 [text] => test [position] => Array ( [lat] => 2 [lon] => 3 ) ) [2] => Array ( [id] => 331 [distance] => 1 [text] => test [position] => Array ( [lat] => 2 [lon] => 3 ) ) )
@ZgrKARALAR did u check the demo?
Thank you so much for helping it work when i delete true in json_decode.
consider accepting the answer, Glad it helped you:)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.