将嵌套数组展平为某个键

时间:2013-05-31 09:23:24

标签: php multidimensional-array flatten

我有这样的数据结构

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中执行此操作?

2 个答案:

答案 0 :(得分:0)

如果我已经理解了你想要的东西,这应该有效:

$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

答案 1 :(得分:0)

最佳解决方案是:

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