繁体   English   中英

PHP打印JSON值

[英]PHP Printing JSON value

我有一个JSON数组,我希望能够向下钻取较低级别并仅打印该值。 当我达到的水平实际上是[0](或[n])时,就会出现问题。 例如,我有以下输出,我只想打印第一个联赛的game key

这就是我尝试打印的方式

HtmlSpecialChars(print_r($user->fantasy_content->users[0]->user[1]->games[0]->game[0]->game_key,1))

但是我不断收到此错误:

Cannot use object of type stdClass as array

当我以增量方式执行此操作时,它似乎在此命令上失败(因此我认为索引不正确):

$user->fantasy_content->users[0]

这是输出:

stdClass Object
(
    [fantasy_content] => stdClass Object
        (
            [xml:lang] => en-US
            [yahoo:uri] => /fantasy/v2/users;use_login=1/games
            [users] => stdClass Object
                (
                    [0] => stdClass Object
                        (
                            [user] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [guid] => IYEZUHTVBYRLIB3OAQC5WRZPQY
                                        )

                                    [1] => stdClass Object
                                        (
                                            [games] => stdClass Object
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [game] => Array
                                                                (
                                                                    [0] => stdClass Object
                                                                        (
                                                                            [game_key] => 147
                                                                            [game_id] => 147
                                                                            [name] => Baseball
                                                                            [code] => mlb
                                                                            [type] => full
                                                                            [url] => http://baseball.fantasysports.yahoo.com/b1
                                                                            [season] => 2006
                                                                        )

                                                                )

                                                        )

                                                    [count] => 1
                                                )

                                        )

                                )

                        )

                    [count] => 1
                )

            [time] => 52.390813827515ms
            [copyright] => Data provided by Yahoo! and STATS, LLC
            [refresh_rate] => 60
        )

)

您可以通过如下方式将stdClass对象转换为数组:

<?php

$array = (array) $myObject;

echo json_encode($array);

您还可以内联转换:

<?php

echo json_encode((array) $object);

对于对象,必须使用->语法,并且如果键/属性名称是数字或具有其他特殊字符,则将需要使用$object->{'0'}语法。

可以使用以下方式检索game_key

$user->fantasy_content->users->{'0'}->user[1]->games->{'0'}->game[0]->game_key;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM