Php 将嵌套数组展平到某个键
我有一个这样的数据结构Php 将嵌套数组展平到某个键,php,multidimensional-array,flatten,Php,Multidimensional Array,Flatten,我有一个这样的数据结构 Array ( [0] => Array ( [actionResult] => Array ( [show_page] => Array ( [15] => Array
Array
(
[0] => Array
(
[actionResult] => Array
(
[show_page] => Array
(
[15] => Array
(
[section_page_id] => 15
[metadata_id] => 62
[section_id] => 7
[display_order] => 0
[current_layout] => 15
[behaviors] => a:1:{i:0;a:1:{i:0;a:0:{}}}
[options] => a:1:{s:13:"defaultLayout";i:63;}
[section_title] => Ask Study
)
[16] => Array
(
[section_page_id] => 16
[metadata_id] => 66
[section_id] => 7
[display_order] => 1
[current_layout] => 16
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:67;}
[section_title] => Ask Study
)
[17] => Array
(
[section_page_id] => 17
[metadata_id] => 69
[section_id] => 7
[display_order] => 2
[current_layout] => 17
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:70;}
[section_title] => Ask Study
)
[18] => Array
(
[section_page_id] => 18
[metadata_id] => 72
[section_id] => 7
[display_order] => 3
[current_layout] => 18
[behaviors] => a:0:{}
[options] => a:1:{s:13:"defaultLayout";i:73;}
[section_title] => Ask Study
)
)
)
)
[1] => Array
(
[actionResult] => Array
(
[view_page] => 18
)
)
)
我需要的是能够将它展平到一个数组结构,直到它在“actionResult”处停止,所有actionResult将成为一个数组,而不是像这样嵌套
在PHP中如何执行此操作?如果我正确理解了您的要求,则此操作应该可以:
$arr2=array();
foreach($arr as $tmp){
foreach($tmp as $actionRequest){
foreach($actionRequest as $key=>$val){
$arr2[$key]=$val;
}
}
}
其中$arr是您已经拥有的,而$arr2将是一个包含2个值的数组,显示页面和查看页面最佳解决方案是:
$new_arr = array_map(function ($a) {
return $a['actionResult'];
}, $old_arr);
你试过什么?为什么。有什么问题?这在php中应该很容易。这不会起作用,因为我可能有上面的列表,还有一个可能没有像你看到的那样的额外内部数组……你的php版本是5.2吗?你能分享你找到的方法吗?