下面是我到目前为止编译的数组的一个示例。我是从一个facebook图形api调用生成数组,并希望删除数组包装每个对象,所以我只是有一个列表下的数据数组。最好我需要一个动态的解决方案,因为他们可能不止一个[0]=> Array, [1] => Array等....
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
)
)
)
)
假设您的数据存储在变量$data
中,您可以这样做:
foreach($data->data as &$el) {
$el = $el[0];
}
现在包装数组已经被移除。
你可以这样做:
if ($data && $data->data){ $new = array_values($data->data); }