简体   繁体   中英

iterate through array with foreach php

Any idea how to access creator with foreach? I receive this as a result from the linkedin groups API and want to iterate through it, after joining the first level with foreach($data as $value) - I am not sure how to iterate through the second and third array to access the values for creator

Array
(
[_total] => 4
[values] => Array
    (
        [0] => Array
            (
                [creator] => Array
                    (
                        [firstName] => Martin
                        [headline] => test1
                        [id] => DNz_ycOHn5
                        [lastName] => asdf
                        [pictureUrl] => https://media.licdn.com
                    )

                [summary] => hi summary
                [title] => hi title
            )

        [1] => Array
            (
                [creator] => Array
                    (
                        [firstName] => Martin
                        [headline] => test2
                        [id] => DNz_ycOHn5
                        [lastName] => asdf
                        [pictureUrl] => https://media.licdn.com
                    )

                [summary] => summary
                [title] => testpost
            )

        [2] => Array
            (
                [creator] => Array
                    (
                        [firstName] => Martin
                        [headline] => test3
                        [id] => DNz_ycOHn5
                        [lastName] => asdf
                        [pictureUrl] => https://media.licdn.com
                    )

                [summary] => 12312
                [title] => linkedinDEV
            )

        [3] => Array
            (
                [creator] => Array
                    (
                        [firstName] => Martin
                        [headline] => test4
                        [id] => DNz_ycOHn5
                        [lastName] => asdf
                        [pictureUrl] => https://media.licdn.com
                    )

                [summary] => test123
                [title] => Discussion Title
            )

    )

  )

best

M

This will create an array of creators:

$creators = array();

foreach($data['values'] as $value) {
    $creators[] = $value['creator'];
}

// An array containing the creator information
var_dump($creators);

从5.5开始,在这种情况下可以使用array_column

$creators =  array_column($data['values'],'creator');

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