dukeraoul Posted October 10, 2007 Share Posted October 10, 2007 I have a multidimensional array full of arrays like this: The arrays represent hierarchy's and can be as deep as necessary. Array ( [0] => Outdoors [1] => Regions [2] => Southern Rivers ) Array ( [0] => Outdoors [1] => Activity [2] => Scenic Viewing ) Array ( [0] => Outdoors [1] => Activity [2] => Fishing ) Array ( [0] => Outdoors [1] => Activity [2] => Camping ) Array ( [0] => Outdoors [1] => Activity [2] => Horseback Riding ) Array ( [0] => Outdoors [1] => Regions [2] => Mountains ) Array ( [0] => Outdoors [1] => Activity [2] => Fishing ) That I want to sort into a single multidimensional array which maintains parent child relations... so some thing like this: Array ( [Outdoors] => Array ( [Activity] => Array ( [0] => Fishing, [1] => Camping, [1] => Scenic Viewing)), [Regions] => Array ( [0] => Mountains, [1] => Southern Rivers )), ) Except that it can expand as deep as the hierarchy demands. I believe that is the result I want although in the end I want to just list the info in the correct hierarchy. I have been stuck on this problem for a while now and any help or insight would be appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/72648-help-building-menu-from-arrays/ Share on other sites More sharing options...
trq Posted October 10, 2007 Share Posted October 10, 2007 have you got any actual code? a specific problem? Or do you just want use to write it for you? Quote Link to comment https://forums.phpfreaks.com/topic/72648-help-building-menu-from-arrays/#findComment-366278 Share on other sites More sharing options...
dukeraoul Posted October 10, 2007 Author Share Posted October 10, 2007 Actually I was looking more for advice on the angle I should attack this problem. I have tried parsing and sorting the data a multiple amount of ways and I keep getting hung up. I guess I should have been more specific. Vague psuedocode would be preferable... but any hint in the right direction would be great. If you want my latest attempt at parsing it I can postthe code, but I am sure its all pretty redundant. Quote Link to comment https://forums.phpfreaks.com/topic/72648-help-building-menu-from-arrays/#findComment-366291 Share on other sites More sharing options...
dukeraoul Posted October 10, 2007 Author Share Posted October 10, 2007 Bump. Really any advice would help. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/72648-help-building-menu-from-arrays/#findComment-366368 Share on other sites More sharing options...
GingerRobot Posted October 10, 2007 Share Posted October 10, 2007 In this case, i personaly favour an approach where you have two levels for each item in your array: a parent and a child. I think this makes it easier to deal with than the case where you have nested parent child relationships within your array. This is probably easier to explain by example. I wrote this a while back for another post: <?php function menu($submenu,$index='Parent'){ foreach($submenu[$index] as $k => $v){ echo "<li><a href='$v'>$k</a></li>"; if(is_array($submenu[$k])){ echo '<ul>'; menu($submenu,$k); echo '</ul>'; } } } $submenu = array(); $submenu["Parent"]["Home"] = "/index.php"; $submenu["Parent"]["Contact Us"] = "/contactus.php"; $submenu["Parent"]["Members"] = "/members/index.php"; $submenu["Home"]["About"] = "/about.php"; $submenu["Contact Us"]["Live Chat"] = "/chat/index.php"; $submenu["Live Chat"]["Live Help"] = "/chat/connected.php"; $submenu["Contact Us"]["Email"] = "/emailform.php"; echo '<ul>'; menu($submenu); echo '</ul>'; ?> Given your current array structure, it would be relatively easy to transform that array to work with the above function. Something like: <?php $array = array(array('Outdoors','Regions','Southern Rivers'),array('Outdoors','Activity','Scenic Viewing')); function createarray($array){ foreach($array as $k => $v){ foreach($v as $k2 => $v2){ if($k2 == 0){ $parent = 'Parent'; }else{ $parent = $array[$k][$k2-1]; } $new_array[$parent][$v2] = $v2; } } return $new_array; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72648-help-building-menu-from-arrays/#findComment-366406 Share on other sites More sharing options...
dukeraoul Posted October 10, 2007 Author Share Posted October 10, 2007 Thank you very much. I will post again if this resolves it. Quote Link to comment https://forums.phpfreaks.com/topic/72648-help-building-menu-from-arrays/#findComment-366416 Share on other sites More sharing options...
dukeraoul Posted October 10, 2007 Author Share Posted October 10, 2007 It did work... Brilliant thank you Very much. Quote Link to comment https://forums.phpfreaks.com/topic/72648-help-building-menu-from-arrays/#findComment-366425 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.