合并/组合数组的键

时间:2014-01-04 20:02:42

标签: php arrays array-merge

我在验证Facebook个人资料时得到以下结果:

Array
(
    [provider] => Facebook
    [uid] => value
    [info] => Array
        (
            [name] => value
            [image] => value
            [nickname] => value
            [first_name] => value
            [last_name] => value
            [location] => value
            [urls] => Array
                (
                    [facebook] => value
                )

        )

    [credentials] => Array
        (
            [token] => SECRET
            [expires] => value
        )

    [raw] => Array
        (
            [id] => value
            [name] => value
            [first_name] => value
            [last_name] => value
            [link] => value
            [username] => value
            [location] => Array
                (
                    [id] => value
                    [name] => value
                )

            [education] => Array
                (
                    [0] => Array
                        (
                            [school] => Array
                                (
                                    [id] => value
                                    [name] => value
                                )

                            [concentration] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => value
                                            [name] => value
                                        )

                                )

                            [type] => value
                        )

                )

            [gender] => value
            [timezone] => value
            [locale] => value
            [verified] => value
            [updated_time] => value
        )

)

问题是我只需要以下密钥:

[provider] => value

[info][image] => value

[credentials][token] => value

[raw][id] => value
[raw][name] => value
[raw][link] => value

如何合并/组合并仅输出这些键?

1 个答案:

答案 0 :(得分:0)

Michael Berkowski在上述评论中提出建议。

foreach ($response as $result) {

    $data = 
        array(
            'provider' => $result['provider'], 
            'image' => $result['info']['image'],
            'token' => $result['credentials']['token'],
            'id' => $result['raw']['id'],
            'name' => $result['raw']['name'],
            'link' => $result['raw']['link'],
        );

}