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

  • SEARCH
  • Home
  • 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 9160563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:46:06+00:00 2026-06-17T13:46:06+00:00

I have a problem building an unordered list from multidimensional array containing entities and

  • 0

I have a problem building an unordered list from multidimensional array containing entities and their children. The problem is I do not want to use recursion as the tree could get very deep and recursion could produce unnecessary load on server.

This is an example of such an array (it is simplified just to contain title and children and the entities could also be objects).

$array = array(
    array('title' => '1', 'children' => array()),
    array('title' => '2', 'children' => array()),
    array('title' => '3', 'children' => array()),
    array('title' => '4', 'children' => array(
        array('title' => '41', 'children' => array()),
        array('title' => '42', 'children' => array()),
        array('title' => '43', 'children' => array()),
        array('title' => '44', 'children' => array(
            array('title' => '441', 'children' => array()),
            array('title' => '442', 'children' => array()),
            array('title' => '443', 'children' => array()),
            array('title' => '444', 'children' => array(
                array('title' => '4441', 'children' => array()),
                array('title' => '4442', 'children' => array()),
                array('title' => '4443', 'children' => array())
            )),
        )),
        array('title' => '45', 'children' => array())
    )),
    array('title' => '5', 'children' => array()),
    array('title' => '6', 'children' => array(
        array('title' => '61', 'children' => array()),
        array('title' => '62', 'children' => array()),
        array('title' => '63', 'children' => array())
    )),
    array('title' => '7', 'children' => array())
);

Doing some research here on SO I came up with this solution that is very close to my desired one:

<html>
<head></head>
<body>
<ul>
<?php
$stack = $array;
$i = 0;
$counts = array();
while(!empty($stack)) {
    $node = array_shift($stack);
    echo "<li>{$node['title']}";
    if($node['children']) {
        echo "<ul>";
        $counts[] = count($node['children']);
        $node['children'] = array_reverse($node['children']);
        foreach($node['children'] as $ch)
            array_unshift($stack, $ch);
    }
    if(!empty($counts)) {
        end($counts);
        if($counts[$key] == 0) {
            echo "</ul>";
            array_pop($counts);
        } else {
            $counts[$key]--;
        }
    }
    if(!$node['children']) {
        echo "</li>";
    }

    // just to make sure we won't end in infinite loop
    $i++;
    if($i == 50) break;
}
?>
</ul>
</body>
</html>

The output is below – as You can see, the problem I have is only the closing </ul> for the sub-trees. My questions: am I overthinking it or am I blind and don’t see an obvious mistake? Could You please push me forward to finite solution or give me Your own?

The output:

  • 1
  • 2
  • 3
  • 4
    • 41
    • 42
    • 43
    • 44
      • 441
      • 442
      • 443
      • 444
        • 4441
        • 4442
        • 4443
      • 45
    • 5
    • 6
      • 61
      • 62
      • 63
  • 7
  • 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-06-17T13:46:08+00:00Added an answer on June 17, 2026 at 1:46 pm

    OK, as it usually goes – ask for a solution You are solving for hours and immediately You are out with one... – so did I some more thinking after a small relax and came up with this solution:

    <html>
    <head></head>
    <body>
    <ul>
    <?php
    $i = 0;
    $counts = array();
    while(!empty($stack)) {
        $node = array_shift($stack);
        if(!empty($counts)) {
            while(end($counts) === 0) {
                $key = key($counts);
                echo "</li></ul>";
                array_pop($counts);
            }
            end($counts);
            $key = key($counts);
            if(isset($counts[$key])) {
                $counts[$key]--;
            }
        }
        echo "<li>{$node['title']}";
        if($node['children']) {
            echo "<ul>";
            $counts[] = count($node['children']);
            $node['children'] = array_reverse($node['children']);
            foreach($node['children'] as $ch)
                array_unshift($stack, $ch);
        }
        if(!$node['children']) {
            echo "</li>";
        }
    
    
        $i++;
        if($i == 50) break;
    }
    ?>
    </ul>
    <p>$i = <?php echo $i; ?></p>
    </body>
    </html>
    

    The problem was I was missing another while that should subtract the $counts value all the way back up the tree…

    I will wait with accepting my own answer just in the case somebody would post their better solution that should be accepted rather than my not so neat solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with building the single executable jar from multiple maven modules. So
I have a problem with building a dll with gcc (mingw). From this site
I have a problem building a HTML table from the following JSON [ {
I have problem, I use mysql++ 3.1.0: after building mysql++.sln in debug and release
I have a problem that is not uncommon when building a plug-in architecture. Assembly
I have a problem with building multi level select box. I have category table
I have a bit of a problem building my project. I'm getting the bellow
I have an old C++ project and I'm having problem building it. For a
I am building multiple forms app in Builder XE2 and I have a problem
I'm building a micro site at the moment and I have a problem. I

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.