0

我有这样的数据结构

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 中这样做???

4

2 回答 2

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 的数组

于 2013-05-31T09:29:59.153 回答
0

最佳解决方案是:

$new_arr = array_map(function ($a) {
    return $a['actionResult'];
}, $old_arr);
于 2013-05-31T10:03:52.903 回答