recursion - PHP - replace array keys recursively and change values to array including old key -


i've spent while trying need old answers haven't quite got (have got close though!).

i have this;

[january] => array     (         [tuesday] => array             (                 [foo] => array                     (                         [82] => 47731                         [125] => 19894                     )                  [bar] => array                     (                         [82] => 29911                         [125] => 10686                     )              )      } 

...and want this;

[0] => array     (        'key' => 'january'        'children' => array             [0] => array                  {                     'key' => 'tuesday'                     'children' => array                           [0] => array                                {                                   'key' => 'foo'                                   'values' => array                                         {                                              [82] => 47731                                              [125] => 19894                                         }                          [1] => array                               {                                   'key' => 'bar'                                   'values' => array                                         {                                              [82] => 29911                                              [125] => 10686                                         }                               }                 )     } 

i've got close adapting first answer recursively change keys in array bottom layer of result correct - nodes keys 'tuesday', 'foo' , 'bar' same in source array.

here's i've got far;

public function transform_hierarchical_output(&$var) {            if (is_array($var))      {         $final = [];          $i = 0;          foreach ($var $k => &$v)          {             $new_node = [                 'key'       => $k,                 'children'  => $v             ];              $k = $i;              $this->transform_hierarchical_output($v);              $final[$k] = $new_node;              $i++;         }          $var = $final;     }      elseif (is_string($var))      {      } } 

this needs work source array of length , depth.

thanks in advance.

geoff

try below:

function t($arr)     {          $a = [];          $num = 0;         foreach($arr $k => $v) {             if (is_array($v))             {                 $a[$num] = [                     'key' => $k,                 ];                 $a[$num][is_array(array_values($v)[0]) ? 'children' : 'values'] = t($v);                 $num ++;             } else {                 $a[$k] = $v;             }         }         return $a;     } 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -