recursion - How to recursively remap array in PHP? -


how recursively remap children nodes? tried writing recursive function, runs 1 iteration. array map - http://php.net/manual/en/function.array-map.php run single dimension

i'm using nested sets model https://github.com/etrepat/baum#getting-started input generated dumping hierarchy

$array = category::where('name', '=', 'all')->first()->getdescendantsandself()->tohierarchy()->toarray();  /* input */ $array = array( 'category_id' => 0, 'children' => array(     array(         'category_id' => 1,         'children' => array(             'category_id' => 2,             'children' => array(                 'category_id' => 3,             )         )     ),     array(         'category_id' => 4,         'children' => array(             'category_id' => 5,             'children' => array(                 'category_id' => 6,             )         )     ) ) ); 

output should

/* $array = array( 'category_id' => 0, 'nodes' => array(     array(         'category_id' => 1,         'nodes' => array(             'category_id' => 2,             'nodes' => array(                 'category_id' => 3,             )         )     ),     array(         'category_id' => 4,         'nodes' => array(             'category_id' => 5,             'nodes' => array(                 'category_id' => 6,             )         )     ) ) );*/   function remap($items){     if(!empty($items['children'])){         $items['nodes'] = $items['children'];         unset($items['children']);         return remap($items['nodes']);     }     else{         return $items;      } } print_r(remap($array)); 

a bit complicated couse , childrens content variates depth

function remap(array &$items){     if(array_key_exists('children',$items)){         $items['nodes'] = $items['children'];         unset($items['children']);         if(array_key_exists('children',$items['nodes'])){             $items['nodes']['nodes'] = $items['nodes']['children'];             unset($items['nodes']['children']);         }         foreach ($items['nodes']  &$x) {             if (is_array($x)) {                 remap($x);             }         }     }     else{         return $items;      } } remap($array); print_r($array); 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -