Accessing JSON array data in PHP

enter image description here

I am trying to get some data from an external API. From the JSON data I want to retrieve some data as marked in the image.

I can access the “teams” data by using this code –

foreach( $data->data as $info ) {
            echo '<li class="team-1">'. $info->teams[0].'</li>';
            echo '<li class="team-2">'. $info->teams[1].'</li>';
 };

But when I try to access data more deep from array of objects it doesn’t work & gives me error –

foreach( $info->sites as $site) {
        foreach( $site->odds as $odd) {
            echo $odd->h2h[0];
        }
}

So my question is what is the best way to loop through the data to access these arrays. I am using this code in WordPress but I think it will be same as PHP.

Read more here:: Accessing JSON array data in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *