【问题标题】:How can i read this stdClass object in php?我如何在 php 中读取这个 stdClass 对象?
【发布时间】:2019-11-21 23:00:29
【问题描述】:
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;

也见前面的例子,但是 什么都没有打印,我不明白我错了什么。有人可以帮我吗,谢谢?谢谢

【问题讨论】:

  • @daremachine 你在哪里看到转储中的索引0

标签: php arrays json object stdclass


【解决方案1】:

leagues 是对象,不是数组,所以需要使用-> 来访问属性。而且由于它是一个数字而不是标识符,因此您必须将属性名称括在大括号中。

echo $data->api->leagues->{"1"}->league_id;

【讨论】:

    【解决方案2】:

    如果把第二个参数json_decode改成true,或许你会这样访问:

    echo $data['api']['leagues']['1']['league_id'];
    

    【讨论】:

    • 这不能回答 OP 的问题
    • 但只能这样?如果我留下它就无法阅读?
    猜你喜欢
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 2011-07-28
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多