在PHP中访问JSON数组数据

时间:2021-11-13 16:55:56

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.

我试图从外部API获取一些数据。从JSON数据我想要检索图像中标记的一些数据。

在PHP中访问JSON数组数据

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.

所以我的问题是循环访问这些数组的数据的最佳方法是什么。我在Wordpress中使用此代码,但我认为它将与PHP相同。

3 个解决方案

#1


0  

You should access h2h directly from odds, since it's not an array, but an object

您应该直接从赔率访问h2h,因为它不是数组,而是对象

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

#2


0  

It looks like odds is an object rather than an array.

看起来赔率是一个对象而不是一个数组。

If h2h will always be the only property in odds then you can try:

如果h2h永远是唯一的赔率属性,那么你可以尝试:

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

Alternatively, if h2h will not always be the only property in odds, then you may want to try casting it into an array:

或者,如果h2h并不总是赔率中的唯一属性,那么您可能想尝试将其转换为数组:

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

#3


0  

You get this json from an external api and convert it using json_decode, right? If that is so, just use the second parameter "$assoc" and set it to true to not get an object, but an associative array that you can use like this:

你从外部api获得这个json并使用json_decode转换它,对吗?如果是这样,只需使用第二个参数“$ assoc”并将其设置为true以不获取对象,而是可以使用的关联数组,如下所示:

$info = json_decode($api_answer,true);
if(isset($info['sites']['odds'])){
     foreach($info['sites']['odds'] as $odd){
        echo $odd['h2h'][0]
     }
}

#1


0  

You should access h2h directly from odds, since it's not an array, but an object

您应该直接从赔率访问h2h,因为它不是数组,而是对象

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

#2


0  

It looks like odds is an object rather than an array.

看起来赔率是一个对象而不是一个数组。

If h2h will always be the only property in odds then you can try:

如果h2h永远是唯一的赔率属性,那么你可以尝试:

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

Alternatively, if h2h will not always be the only property in odds, then you may want to try casting it into an array:

或者,如果h2h并不总是赔率中的唯一属性,那么您可能想尝试将其转换为数组:

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

#3


0  

You get this json from an external api and convert it using json_decode, right? If that is so, just use the second parameter "$assoc" and set it to true to not get an object, but an associative array that you can use like this:

你从外部api获得这个json并使用json_decode转换它,对吗?如果是这样,只需使用第二个参数“$ assoc”并将其设置为true以不获取对象,而是可以使用的关联数组,如下所示:

$info = json_decode($api_answer,true);
if(isset($info['sites']['odds'])){
     foreach($info['sites']['odds'] as $odd){
        echo $odd['h2h'][0]
     }
}