简体   繁体   中英

Looping through JSON object properties

I'm totally newbie what comes to programming so i'm not even quite sure if my terms are right but i would like to get some hints and tips what is best practice to loop through JSON object? Let's say i want all game names from following JSON print_r output.

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
                                (
                                )

                        )

                )

        )

)

I can access single line (not sure if this is best practise either):

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

But i'm more than clueless how to loop through all game names.

Any help much appreciated!

The "name"s are accessed using $OBJ->top[0]->game->name etc... So just foreach over the "top" array:

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

Create an empty array, loop the objects top array and fill your empty array:

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

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

When you load your JSON string into PHP you can use:

json_decode($string_of_json, true);

The true flag will load it into an array you can loop through using, for example, foreach .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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