php - Merging flat array with itself to build a multidimensional array recursively -


trying multidimensional array flat data

available raw data

this raw data ist available me. need build multidimensional array children stored within respective parents.

array (     [index] => array     (         [slug] => index         [parent_slug] =>      )     [praxis-und-team] => array     (         [slug] => praxis-und-team         [parent_slug] =>      )     [leistungen] => array     (         [slug] => leistungen         [parent_slug] => praxis-und-team     )     [partner-und-netzwerk] => array     (         [slug] => partner-und-netzwerk         [parent_slug] =>      )     [notfall] => array     (         [slug] => notfall         [parent_slug] =>      )     [impressum] => array     (         [slug] => impressum         [parent_slug] => leistungen     ) ) 

needed data

it's slug/parent_slug pairing. there might more sublevels, has recursive until parent_slug == '' reached @ topmost level. output should like:

array (     [index] => array     (         [slug] => index         [parent_slug] =>      )     [praxis-und-team] => array     (         [slug] => praxis-und-team         [parent_slug] =>          [children] => array         (             [leistungen] => array             (                 [slug] => leistungen                 [parent_slug] => praxis-und-team                 [children] => array                 (                     [impressum] => array                     (                         [slug] => impressum                         [parent_slug] => leistungen                     )                 )             )         )     )     [partner-und-netzwerk] => array     (         [slug] => partner-und-netzwerk         [parent_slug] =>      )     [notfall] => array     (         [slug] => notfall         [parent_slug] =>      ) ) 

thank in advance help! sorry if failed make light of problem. i‘ve been looking @ long …

assumption: entries in array outermost innermost. revert original array, walk thru in 1 pass, restructure it, , revert again (if necessary).

code

    <?php     $myarray = array(         'index' => array(             'slug' => 'index',             'parent_slug' => '',         ),         'praxis-und-team' => array (             'slug' => 'praxis-und-team',             'parent_slug' => '',         ),         'leistungen' => array(             'slug' => 'leistungen',             'parent_slug' => 'praxis-und-team',         ),         'partner-und-netzwerk' => array (             'slug' => 'partner-und-netzwerk',             'parent_slug' => '',         ),         'notfall' => array(             'slug' => 'notfall',             'parent_slug' => '',         ),         'impressum' => array (             'slug' => 'impressum',             'parent_slug' => 'leistungen',         ),     );      echo '<pre>'; var_dump($myarray); echo '</pre>';      // want work bottom top     $myarray = array_reverse($myarray, true);      foreach($myarray $key => $item) {         if (!empty($myarray[$key]['parent_slug'])) {             $myarray[$myarray[$key]['parent_slug']]['children'][$key] = $myarray[$key];             unset($myarray[$key]);         }     }      // reverse array again.     $myarray = array_reverse($myarray, true);      echo '<pre>'; var_dump($myarray); echo '</pre>';       ?> 

output

    array(4) {       ["index"]=>       array(2) {         ["slug"]=>string(5) "index"         ["parent_slug"]=>string(0) ""       }       ["praxis-und-team"]=>       array(3) {         ["slug"]=>string(15) "praxis-und-team"         ["parent_slug"]=>string(0) ""         ["children"]=>         array(1) {           ["leistungen"]=>           array(3) {             ["slug"]=>string(10) "leistungen"             ["parent_slug"]=>string(15) "praxis-und-team"             ["children"]=>             array(1) {               ["impressum"]=>               array(2) {                 ["slug"]=>string(9) "impressum"                 ["parent_slug"]=>string(10) "leistungen"               }             }           }         }       }       ["partner-und-netzwerk"]=>       array(2) {         ["slug"]=>string(20) "partner-und-netzwerk"         ["parent_slug"]=>string(0) ""       }       ["notfall"]=>       array(2) {         ["slug"]=>string(7) "notfall"         ["parent_slug"]=>string(0) ""       }     } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -