我如何在 php 中读取这个 stdClass 对象?
How can i read this stdClass object in php?
stdClass Object (
[api] => stdClass Object (
[results] => 1
[leagues] => stdClass Object (
[1] => stdClass Object (
[league_id] => 1
[name] => World Cup
[country] => World
[country_code] =>
[season] => 2018
[season_start] => 2018-06-14
[season_end] => 2018-07-15
[logo] => https://media.api-football.com/leagues/1.png
[flag] =>
[standings] => 1
)
)
)
)
$data= json_decode($response,false);
我必须阅读元素 [league_id]
我试着写
echo $data->api->leagues[1]->league_id;
也看到前面的例子,但是
什么都没有打印,我不明白我错了什么。有人可以帮助我吗,谢谢?谢谢
如果把第二个参数json_decode
改成true,也许你会这样访问:
echo $data['api']['leagues']['1']['league_id'];
leagues
是对象,不是数组,所以需要用->
访问属性。由于它是一个数字而不是标识符,因此您必须将 属性 名称括在大括号中。
echo $data->api->leagues->{"1"}->league_id;
stdClass Object (
[api] => stdClass Object (
[results] => 1
[leagues] => stdClass Object (
[1] => stdClass Object (
[league_id] => 1
[name] => World Cup
[country] => World
[country_code] =>
[season] => 2018
[season_start] => 2018-06-14
[season_end] => 2018-07-15
[logo] => https://media.api-football.com/leagues/1.png
[flag] =>
[standings] => 1
)
)
)
)
$data= json_decode($response,false);
我必须阅读元素 [league_id]
我试着写
echo $data->api->leagues[1]->league_id;
也看到前面的例子,但是 什么都没有打印,我不明白我错了什么。有人可以帮助我吗,谢谢?谢谢
如果把第二个参数json_decode
改成true,也许你会这样访问:
echo $data['api']['leagues']['1']['league_id'];
leagues
是对象,不是数组,所以需要用->
访问属性。由于它是一个数字而不是标识符,因此您必须将 属性 名称括在大括号中。
echo $data->api->leagues->{"1"}->league_id;