循环通过JSON对象属性

时间:2014-03-18 16:48:02

标签: php json

我是完全新手的编程所以我甚至不确定我的条款是否正确但我想得到一些提示和提示循环JSON对象的最佳做法是什么?假设我想要跟随JSON print_r输出的所有游戏名称。

stdClass Object
(
    [_total] => 555
    [_links] => stdClass Object
        (
            [self] => https://api.twitch.tv/kraken/games/top?limit=2&offset=0
            [next] => https://api.twitch.tv/kraken/games/top?limit=2&offset=2
        )

    [top] => Array
        (
            [0] => stdClass Object
                (
                    [viewers] => 86386
                    [channels] => 1159
                    [game] => stdClass Object
                        (
                            [name] => League of Legends
                            [_id] => 21779
                            [giantbomb_id] => 24024
                            [box] => stdClass Object
                                (
                                    [template] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-{width}x{height}.jpg
                                    [small] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-52x72.jpg
                                    [medium] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-136x190.jpg
                                    [large] => http://static-cdn.jtvnw.net/ttv-boxart/League%20of%20Legends-272x380.jpg
                                )

                            [logo] => stdClass Object
                                (
                                    [template] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-{width}x{height}.jpg
                                    [small] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-60x36.jpg
                                    [medium] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-120x72.jpg
                                    [large] => http://static-cdn.jtvnw.net/ttv-logoart/League%20of%20Legends-240x144.jpg
                                )

                            [_links] => stdClass Object
                                (
                                )

                        )

                )

            [1] => stdClass Object
                (
                    [viewers] => 17288
                    [channels] => 162
                    [game] => stdClass Object
                        (
                            [name] => Hearthstone: Heroes of Warcraft
                            [_id] => 138585
                            [giantbomb_id] => 42033
                            [box] => stdClass Object
                                (
                                    [template] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-{width}x{height}.jpg
                                    [small] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-52x72.jpg
                                    [medium] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-136x190.jpg
                                    [large] => http://static-cdn.jtvnw.net/ttv-boxart/Hearthstone%3A%20Heroes%20of%20Warcraft-272x380.jpg
                                )

                            [logo] => stdClass Object
                                (
                                    [template] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-{width}x{height}.jpg
                                    [small] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-60x36.jpg
                                    [medium] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-120x72.jpg
                                    [large] => http://static-cdn.jtvnw.net/ttv-logoart/Hearthstone%3A%20Heroes%20of%20Warcraft-240x144.jpg
                                )

                            [_links] => stdClass Object
                                (
                                )

                        )

                )

        )

)

我可以访问单行(不确定这是否是最佳做法):

$OBJ->method()->top[0]->game->name;

但我不知道如何循环播放所有游戏名称。

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:2)

使用$OBJ->top[0]->game->name等访问“名称”...只需foreach在“top”数组上:

foreach($OBJ->top as $object) {
    echo $object->game->name;
}

答案 1 :(得分:1)

创建一个空数组,将对象循环到数组并填充空数组:

$allgames=array();
foreach($OBJ->method()->top as $ob){

    $allgames[] = $ob->game->name;
}

答案 2 :(得分:1)

将JSON字符串加载到PHP时,可以使用:

json_decode($string_of_json, true);

true标志会将其加载到您可以循环使用的数组中,例如foreach