Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1107171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:54:02+00:00 2026-05-17T01:54:02+00:00

Based on this question: Getting a modified preorder tree traversal model (nested set) into

  • 0

Based on this question:
Getting a modified preorder tree traversal model (nested set) into a <ul>

The logic bellow is used to build an ordered list, but how to do the same with an array?

I want to build a nested array.

// bootstrap loop
$result = '';
$currDepth = -1;  // -1 to get the outer <ul>
while (!empty($tree)) {
  $currNode = array_shift($tree);
  // Level down?
  if ($currNode['depth'] > $currDepth) {
    // Yes, open <ul>
    $result .= '<ul>';
  }
  // Level up?
  if ($currNode['depth'] < $currDepth) {
    // Yes, close n open <ul>
    $result .= str_repeat('</ul>', $currDepth - $currNode['depth']);
  }
  // Always add node
  $result .= '<li>' . $currNode['title'] . '</li>';
  // Adjust current depth
  $currDepth = $currNode['depth'];
  // Are we finished?
  if (empty($tree)) {
    // Yes, close n open <ul>
    $result .= str_repeat('</ul>', $currDepth + 1);
  }
}

print $result;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:54:03+00:00Added an answer on May 17, 2026 at 1:54 am

    Ah, finally something for which references are handy:

    <?php
    $tree = array(
        array('Cat 1', 'depth' => 0),
        array('Cat 2', 'depth' => 1),
        array('Cat 3', 'depth' => 1),
        array('Cat 4', 'depth' => 2),
        array('Cat 5', 'depth' => 1),
        array('Cat 6', 'depth' => 2),
        array('Cat 7', 'depth' => 3),
        array('Cat 8', 'depth' => 1)
    );
    //same as before
    $currDepth = -1;
    
    //initilialize result
    $result = array();
    
    //create path structure for depths
    $path = array();
    
    //create 'root' node
    $olditem = array('children'=> &$result);
    
    
    foreach($tree as $item){
        if($item['depth'] > $currDepth){
            //remove possible old reference (old depth of other branch
            if(isset($path[$item['depth']])) unset($path[$item['depth']]);
    
            //make sure we have an array entry
            if(!isset($olditem['children'])) $olditem['children'] = array();
    
            //acquire target
            $path[$item['depth']] = &$olditem['children'];
        }
        if($item['depth'] != $currDepth) unset($olditem);
        //set correct target
        $currDepth = $item['depth'];
        //add item
        $path[$currDepth][] = &$item;
        //copy & remove reference
        $olditem = &$item;
        unset($item);
    }
    //always nice to clean up reference bombs:
    unset($path);
    unset($olditem);
    
    var_dump($result);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on this question it appears that the default template for CheckStyle will allow
Based on this question I don't want to litter my ready stuff waiting for
This question is based on another question of mine (thankfully answered). So if in
Based on the response to this question: Why does C++ have header files and
The same as this question but for java Update Based on the comments and
Being new to test based development, this question has been bugging me. How much
Ok this is more of a computer science question, than a question based on
Hey guys. This is a follow-on from this question : After getting the right
UPDATE (2010-12-21): Completely rewrote this question based on tests that I've been doing. Also,
edit : I based this question on a false assumption - that the generic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.