如何合并此数组或删除键

时间:2016-10-16 20:32:00

标签: php multidimensional-array array-merge

下面是我到目前为止编译的数组的示例。我从facebook图形api调用生成数组,并希望删除包装每个对象的数组,所以我只在数据数组下有一个列表。我最好需要动态解决方案,因为它们可能不止一个[0] =>数组,[1] =>数组等等......在每个API请求中。

stdClass Object
(
    [data] => Array
        (
            [0] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 21744379694_10154626935079695
                            [created_time] => 2016-10-16T06:29:28+0000
                            [from] => stdClass Object
                                (
                                    [name] => Tony Hawk
                                    [id] => 21744379694
                                )
                        )
                )

            [1] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 50043151918_10154176205946919
                            [created_time] => 2016-10-15T20:04:22+0000
                            [from] => stdClass Object
                                (
                                    [name] => GoPro
                                    [id] => 50043151918
                                )

                        )
                )
        )
)

我希望这个数组最终看起来像这样。这里最好的方法是什么?

stdClass Object
(
    [data] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 21744379694_10154626935079695
                    [created_time] => 2016-10-16T06:29:28+0000
                    [from] => stdClass Object
                        (
                            [name] => Tony Hawk
                            [id] => 21744379694
                        )
                )
            [1] => stdClass Object
                (
                    [id] => 50043151918_10154176205946919
                    [created_time] => 2016-10-15T20:04:22+0000
                    [from] => stdClass Object
                        (
                            [name] => GoPro
                            [id] => 50043151918
                        )

                )
        )
)

2 个答案:

答案 0 :(得分:2)

假设您的数据存储在变量$data中,您可以执行以下操作:

foreach($data->data as &$el) {
    $el = $el[0];
}

现在已经删除了包装数组。

eval.in

上查看它

答案 1 :(得分:0)

您可以使用:

if ($data && $data->data){ $new = array_values($data->data); }