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 94853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:34:00+00:00 2026-05-10T23:34:00+00:00

How do you build a hierarchical set of tags with data in PHP? For

  • 0

How do you build a hierarchical set of tags with data in PHP?

For example, a nested list:

<div>     <ul>         <li>foo         </li>         <li>bar             <ul>                 <li>sub-bar                 </li>             </ul>         </li>     </ul> </div> 

This would be build from flat data like this:

nested_array = array(); nested_array[0] = array('name' => 'foo', 'depth' => 0) nested_array[1] = array('name' => 'bar', 'depth' => 0) nested_array[2] = array('name' => 'sub-bar', 'depth' => 1) 

It would be nice if it were nicely formatted like the example, too.

  • 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. 2026-05-10T23:34:00+00:00Added an answer on May 10, 2026 at 11:34 pm

    Edit: Added formatting

    As already said in the comments, your data structure is somewhat strange. Instead of using text manipulation (like OIS), I prefer DOM:

    <?php  $nested_array = array(); $nested_array[] = array('name' => 'foo', 'depth' => 0); $nested_array[] = array('name' => 'bar', 'depth' => 0); $nested_array[] = array('name' => 'sub-bar', 'depth' => 1); $nested_array[] = array('name' => 'sub-sub-bar', 'depth' => 2); $nested_array[] = array('name' => 'sub-bar2', 'depth' => 1); $nested_array[] = array('name' => 'sub-sub-bar3', 'depth' => 3); $nested_array[] = array('name' => 'sub-sub3', 'depth' => 2); $nested_array[] = array('name' => 'baz', 'depth' => 0);  $doc = new DOMDocument('1.0', 'iso-8859-1'); $doc->formatOutput = true; $rootNode = $doc->createElement('div'); $doc->appendChild($rootNode);  $rootList = $doc->createElement('ul'); $rootNode->appendChild($rootList);  $listStack = array($rootList); // Stack of created XML list elements $depth = 0; // Current depth  foreach ($nested_array as $nael) {     while ($depth < $nael['depth']) {         // New list element         if ($listStack[$depth]->lastChild == null) {             // More than one level at once             $li = $doc->createElement('li');             $listStack[$depth]->appendChild($li);         }         $listEl = $doc->createElement('ul');         $listStack[$depth]->lastChild->appendChild($listEl);         array_push($listStack, $listEl);          $depth++;     }      while ($depth > $nael['depth']) {         array_pop($listStack);         $depth--;     }      // Add the element itself     $li = $doc->createElement('li');     $li->appendChild($doc->createTextNode($nael['name']));     $listStack[$depth]->appendChild($li); }  echo $doc->saveXML(); 

    Your formatting convention is kind of strange. Replace the last line with the following to achieve it:

    printEl($rootNode);  function printEl(DOMElement $el, $depth = 0) {     $leftFiller = str_repeat('\t', $depth);     $name = preg_replace('/[^a-zA-Z]/', '', $el->tagName);      if ($el->childNodes->length == 0) {         // Empty node         echo $leftFiller . '<' . $name . '/>\n';     } else {         echo $leftFiller . '<' . $name . '>';         $printedNL = false;          for ($i = 0;$i < $el->childNodes->length;$i++) {             $c = $el->childNodes->item($i);              if ($c instanceof DOMText) {                 echo htmlspecialchars($c->wholeText);             } elseif ($c instanceof DOMElement) {                 if (!$printedNL) {                     $printedNL = true;                     echo '\n';                 }                 printEl($c, $depth+1);             }         }          if (!$printedNL) {             $printedNL = true;             echo '\n';         }          echo $leftFiller . '</' . $name . '>\n';     }  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 267k
  • Answers 267k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here a simple solution using basic inheritance and even without… May 13, 2026 at 12:50 pm
  • Editorial Team
    Editorial Team added an answer If you have a handle of the HttpServletRequest, then you… May 13, 2026 at 12:50 pm
  • Editorial Team
    Editorial Team added an answer There's an easy way to ensure this: Don't mess around… May 13, 2026 at 12:50 pm

Related Questions

How do you build a Maven project without running unit tests? Currently restructuring some
How would you build a price comparison script? I know Amazon offers a public
I was asked to build a control-system for a Ebay-like Finnish auction-site huuto.net .
If I notice that a hash table (or any other data structure built on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.