将嵌套数组平铺到某个键



我有一个这样的数据结构

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个值的数组,Show_Page和view_page

最佳解决方案是:

$new_arr = array_map(function ($a) {
    return $a['actionResult'];
}, $old_arr);

相关内容

  • 没有找到相关文章

最新更新