0

I have a data structure like this

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
                )

        )

)

What i need is the ability to flatten this to an array structure to a point where it stops at "actionResult" where all the actionResult will become ONE array rather than nested like this...

How can I go about by doing this in PHP???

RST
  • 3,899
  • 2
  • 20
  • 33
Benjamin
  • 429
  • 4
  • 17

2 Answers2

0

if i have understood what you want correctly this should work:

$arr2=array();

foreach($arr as $tmp){
  foreach($tmp as $actionRequest){
    foreach($actionRequest as $key=>$val){
      $arr2[$key]=$val;
    }
  }
}

where $arr is what you already have and $arr2 will be an array including 2 values , Show_Page and view_page

Bor691
  • 606
  • 1
  • 11
  • 21
  • This will not work because I may have that list above and have another one that may not have an extra inner array like your seeing... – Benjamin Jun 03 '13 at 19:05
0

Best solution is:

$new_arr = array_map(function ($a) {
    return $a['actionResult'];
}, $old_arr);
mpyw
  • 5,526
  • 4
  • 30
  • 36