首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >循环遍历JSON对象属性

循环遍历JSON对象属性
EN

Stack Overflow用户
提问于 2014-03-18 16:48:02
回答 3查看 209关注 0票数 0

对于编程,我完全是新手,所以我甚至不确定我的条款是否正确,但是我想得到一些提示和提示,什么是最佳实践来循环JSON对象?假设我希望所有游戏名都来自于JSON的print_r输出。

代码语言:javascript
复制
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
                                (
                                )

                        )

                )

        )

)

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

代码语言:javascript
复制
$OBJ->method()->top[0]->game->name;

但我不知道怎么循环所有的游戏名称。

任何帮助都非常感谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-03-18 16:52:02

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

代码语言:javascript
复制
$allgames=array();
foreach($OBJ->method()->top as $ob){

    $allgames[] = $ob->game->name;
}
票数 1
EN

Stack Overflow用户

发布于 2014-03-18 16:51:55

“名称”是使用$OBJ->top[0]->game->name等访问的.因此,只需将foreach放在"top“数组上:

代码语言:javascript
复制
foreach($OBJ->top as $object) {
    echo $object->game->name;
}
票数 2
EN

Stack Overflow用户

发布于 2014-03-18 16:52:41

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

代码语言:javascript
复制
json_decode($string_of_json, true);

true标志将加载到一个数组中,例如,可以使用foreach循环。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22485795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档