简体   繁体   中英

Loop through a JSON decoded array

I am using randonuser API to generate dummy images. The API returns JSON that I have used json_decode to decode and using print_r , I got the array below:

Array
(
    [results] => Array
        (
            [0] => Array
                (
                    [user] => Array
                        (
                            [gender] => male
                            [name] => Array
                                (
                                    [title] => mr
                                    [first] => dwight
                                    [last] => evans
                                )

                            [location] => Array
                                (
                                    [street] => 6822 hunters creek dr
                                    [city] => fresno
                                    [state] => vermont
                                    [zip] => 63409
                                )

                            [email] => dwight.evans44@example.com
                            [username] => ticklishostrich542
                            [password] => ebony
                            [salt] => 4xuAIjmh
                            [md5] => 648f472ff152a194c410d774ff9a4b9d
                            [sha1] => f23cc7ffd2b8980d10de86bccc85068ecf9b7b45
                            [sha256] => fec06f7df352a06aab9c30af9d7ab9b5b81dc0bd6b7567b59fba1a731dea6bba
                            [registered] => 1129218274
                            [dob] => 409533355
                            [phone] => (797)-563-6160
                            [cell] => (200)-718-4014
                            [SSN] => 213-46-5200
                            [picture] => Array
                                (
                                    [large] => http://api.randomuser.me/portraits/men/98.jpg
                                    [medium] => http://api.randomuser.me/portraits/med/men/98.jpg
                                    [thumbnail] => http://api.randomuser.me/portraits/thumb/men/98.jpg
                                )

                            [version] => 0.4.1
                        )

                    [seed] => cf744a697a08f256
                )

           . .. .....

and so on. 

I just need the large key value under parent picture . How do I loop through it using a foreach statement?

Just access it as you normally would:

$data = json_decode('that json string', true);
foreach($data['results'] as $value) {
    echo $value['user']['picture']['large'];
}

使用json_decode($var, true) ,那么您将拥有一个数组,并且循环将更容易。

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