JasonLewis Posted January 20, 2010 Share Posted January 20, 2010 I'm absolutely stumped, it's 1am and my brain is pretty much fried from staring at this. I can't get my head around it, so hopefully someone here can. I'm building a dynamic menu with nested items which can be nested an infinite amount of times. I've managed to pull the data from the database and organize it into a multi-dimensional array, however now I want to turn this array into a list with the items properly nested. I thought this would be the easy part, but apparently not. Here is an example of the array: Array ( [1] => Array ( [PAGE_ID] => 1 [PARENT_ID] => 0 [AUTHOR_ID] => 1 [page_name] => Example Page 01 [page_permalink] => example-page-01 [page_body] => This is an example page. [page_visibility] => 1 [page_creation_date] => 2010-01-20 14:29:09 [page_modification_date] => 0000-00-00 00:00:00 [children] => Array ( [2] => Array ( [PAGE_ID] => 2 [PARENT_ID] => 1 [AUTHOR_ID] => 1 [page_name] => Example Page 02 [page_permalink] => example-page-02 [page_body] => This is an example page. [page_visibility] => 1 [page_creation_date] => 2010-01-20 14:29:38 [page_modification_date] => 0000-00-00 00:00:00 [children] => Array ( [3] => Array ( [PAGE_ID] => 3 [PARENT_ID] => 2 [AUTHOR_ID] => 1 [page_name] => Example Page 03 [page_permalink] => example-page-03 [page_body] => This is an example page. [page_visibility] => 1 [page_creation_date] => 2010-01-20 14:31:47 [page_modification_date] => 0000-00-00 00:00:00 ) ) ) [4] => Array ( [PAGE_ID] => 4 [PARENT_ID] => 1 [AUTHOR_ID] => 1 [page_name] => Example Page 04 [page_permalink] => example-page-04 [page_body] => An example page. [page_visibility] => 1 [page_creation_date] => 2010-01-20 22:05:00 [page_modification_date] => 0000-00-00 00:00:00 ) ) ) ) 1 Currently I have nothing, since I have been getting no where with it. If you can provide me with the theory behind it then that would be great. At the moment I know I need some kind of recursive function, but I'm still stumped. Regards, Jason Lewis Link to comment https://forums.phpfreaks.com/topic/189155-multi-dimensional-array-to-html/ Share on other sites More sharing options...
Buddski Posted January 20, 2010 Share Posted January 20, 2010 $menu = ''; function iterateMenu($array,&$output) { $output .= '<ul style="list-style:none;">'; foreach ($array as $values) { $output .= '<li>'.$values['PAGE_NAME'].'</li>'; if (isset($values['children']) && is_array($values['children'])) { $output .= '<li>'; iterateMenu($values['children'],$output); $output .= '</li>'; } } $output .= '</ul>'; } iterateMenu($a,$menu); echo $menu; Should get you started Link to comment https://forums.phpfreaks.com/topic/189155-multi-dimensional-array-to-html/#findComment-998662 Share on other sites More sharing options...
JasonLewis Posted January 22, 2010 Author Share Posted January 22, 2010 Cheers, I've been starting to do it like this then I think, no that can't work. However, I've managed to get it working now with the theory that you've supplied. However. I still can't get my head around it, maybe I'm just being arrogant. The way I see this theory is that it'll detect that a parent has children, then go into the array more and if the parent had another parent after it, then it would be skipped. But it's not how it works, and I can't figure out how it is working. Again, I think I'm looking to hard at it and not seeing the clear picture. Thanks though, I've got it to display correctly, I just need to know HOW it displays it correctly now. Link to comment https://forums.phpfreaks.com/topic/189155-multi-dimensional-array-to-html/#findComment-999730 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.