stopblackholes Posted April 18, 2008 Share Posted April 18, 2008 I've been messing around with this for awhile. What i'm trying to do is get the array to output in a function using div tags instead of formatting it in a list. like so <ul><li> no good </li><ul> <div class="topic"> ok</div> <div class="reply"> ok</div> but i have to order then check the parent id then if it has parent output reply. Also probably use depth of reply by checking id against parent id so i can adjust the margins. right now im just using a messy recursive function. any ideas? <?php $tree = array( array( id => "1", name => "1st topic", parentid => "0" ), array( id => "2", name => "reply", parentid => "1" ), array( id => "3", name => "reply", parentid => "1" ), array( id => "4", name => "2nd topic", parentid => "0" ), array( id => "5", name => "reply 2nd", parentid => "4" ), array( id => "6", name => "reply", parentid => "1" ) ); function itemsFormat($parent, $tree) { foreach ($tree as $item) { if($parent == $item['parentid']) { $output .= '<ul>'; $output .= '<li>' . $item['name'] . '</li>'; $output .= itemsFormat($item['id'], $tree); $output .= '</ul>'; } } return $output; } echo itemsFormat(0, $tree); ///output raw array for testing print '<pre>'; var_dump($tree); print '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/101692-two-dimensional-array-nesting-posts-problem/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.