使用foreach php迭代数组

时间:2015-01-22 00:19:35

标签: php foreach linkedin

知道如何使用foreach访问创建者吗?我收到这个来自linkedin groups API的结果,并希望在使用foreach($ data as $ value)加入第一级后迭代它 - 我不知道如何遍历第二个和第三个数组来访问值对于创作者

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
            )

    )

  )

最好

中号

2 个答案:

答案 0 :(得分:1)

这将创建一个创建者数组:

$creators = array();

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

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

答案 1 :(得分:1)

从5.5开始,您可以使用array_column

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