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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:45:45+00:00 2026-06-14T00:45:45+00:00

I try to make a tree list in PHP from a hierarchy stored in

  • 0

I try to make a tree list in PHP from a hierarchy stored in a concatenated string in my mysql database

This my table :

Screenshot of table

and I’d like to reproduce something like this :

<ul>
  <li>
    <ul>
      <li></li>
      <li>
          <ul>
              <li></li>
              <li></li>
           </ul>
      </li>
      <li></li>
    </ul>
  </li>
  <li></li>
  <li></li>
</ul>

I know I have to use a recursive function I don’t reach to do…

Maybe someone could help me

  • 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-14T00:45:46+00:00Added an answer on June 14, 2026 at 12:45 am

    code without comments

    see the usage and dataset section below to see what you need to pass and how to use these functions:

    function items_to_tree( $items ){
      $array = array();
      foreach( $items as $item ) {
        $parts = explode('.', $item['hierarchy']);
        $last = array_pop( $parts );
        $cursor = &$array;
        foreach ( $parts as $part ) {
          if ( !is_array($cursor[$part]) ) {
            $cursor[$part] = array();
          }
          $cursor = &$cursor[$part];
        }
        $cursor[$last]['#item'] = $item;
      }
      return $array;
    }
    
    function tree_to_ul( $tree ){
      $html = $children = '';
      foreach( $tree as $key => $item ){
        if ( substr($key,0,1) == '#' ) continue;
        $children .= tree_to_ul( $item );
      }
      if ( isset($tree['#item']) ) {
        $html .= '<li>' . PHP_EOL;
        $html .= '<em>' . $tree['#item']['menu_text'] . '</em>' . PHP_EOL;
        $html .= ( $children ? '<ul>' . $children . '</ul>' . PHP_EOL : '' );
        $html .= '</li>' . PHP_EOL;
        return $html;
      }
      else {
        return $children;
      }
    }
    

    code with comments and explanation

    The code to convert your items to a tree structure:

    function items_to_tree( $items ){
      $array = array();
      foreach( $items as $item ) {
        /// split each hierarchy string into it's dot separated parts
        $parts = explode('.', $item['hierarchy']);
        /// pop off the last item of the array, we'll use this for assignment later
        $last = array_pop( $parts );
        /// create a reference to our position in the array we wish to fill out
        $cursor = &$array;
        /// step each hierarchy part and travel down the array structure, 
        /// just like you would if you typed an array path manually. 
        /// i.e. $array[$part][$part][...] and so on
        foreach ( $parts as $part ) {
          /// if at this point in the array, we don't have an array, make one.
          if ( !is_array($cursor[$part]) ) {
            $cursor[$part] = array();
          }
          /// ready for the next step, shift our reference to point to the next
          /// $part in the array chain. e.g. if $cursor pointed to `$array[$part]`
          /// before, after the next line of code the $cursor will point 
          /// to `$array[$oldpart][$part]`
          $cursor = &$cursor[$part];
        }
        /// we popped the last item off the $parts array so we could easily
        /// assign our final value to where the $cursor ends up pointing to.
        /// starting with a hierarchy of '00001.00002.00003' would mean at this
        /// point $cursor points to $array['00001']['00002'] and $last = '00003';
        /// so finally we get $array['00001']['00002']['00003']['#item'] = $item;
        $cursor[$last]['#item'] = $item;
        /// use '#item' to keep our item's information separate from it's children.
      }
      /// return our built up array.
      return $array;
    }
    

    The code to convert the tree structure to a UL:

    function tree_to_ul( $tree ){
      /// start with nothing
      $html = $children = '';
      /// step each item found in the current level of $tree
      foreach( $tree as $key => $item ){
        /// if the item's key starts with a # skip, these contain
        /// our item's information and should not be treated as children
        if ( substr($key,0,1) == '#' ) continue;
        /// recurse this function so that we do the same for any child @ any level.
        $children .= tree_to_ul( $item );
      }
      /// if at this level a #item has been set, use this item information to
      /// add a title to our level. You could change this to add whatever info
      /// from your original database item that you'd like.
      if ( isset($tree['#item']) ) {
        $html .= '<li>' . PHP_EOL;
        $html .= '<em>' . $tree['#item']['menu_text'] . '</em>' . PHP_EOL;
        $html .= ( $children ? '<ul>' . $children . '</ul>' . PHP_EOL : '' );
        $html .= '</li>' . PHP_EOL;
        return $html;
      }
      /// if there wasn't an item, just return the traversed children.
      else {
        return $children;
      }
    }
    

    dataset:

    /// I simplified your dataset to an array, this could easily be generated
    /// from a database query. You could also convert my code so that you
    /// don't have to pre-generate an array, and instead could process after
    /// each fetch from the database.
    
    $items = array(
      array('hierarchy' => '00001',             'menu_text' => 'One'),
      array('hierarchy' => '00002',             'menu_text' => 'Two'),
      array('hierarchy' => '00002.00001',       'menu_text' => 'Three'),
      array('hierarchy' => '00002.00002',       'menu_text' => 'Four'),
      array('hierarchy' => '00002.00003',       'menu_text' => 'Five'),
      array('hierarchy' => '00002.00004',       'menu_text' => 'Six'),
      array('hierarchy' => '00003',             'menu_text' => 'Seven'),
      array('hierarchy' => '00003.00001',       'menu_text' => 'Eight'),
      array('hierarchy' => '00003.00001.00001', 'menu_text' => 'Nine'),
      array('hierarchy' => '00003.00001.00002', 'menu_text' => 'Ten'),
      array('hierarchy' => '00003.00001.00003', 'menu_text' => 'Eleven'),
      array('hierarchy' => '00003.00002',       'menu_text' => 'Twelve'),
    );
    

    usage:

    /// Simple usage :) if a little complex explanation
    
    $tree = items_to_tree( $items );
    $html = tree_to_ul( $tree );
    
    echo $html;
    

    in the interests of codegolf 😉

    The following could replace my items_to_tree function — however it isn’t advised.

    $a = array();
    foreach($items as $i){
     eval('$a["'.str_replace('.','"]["',$i['hierarchy']).'"]=array("#item"=>$i);');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this problem in codeigniter: I try to make a navigation tree system
I try to make a table header fixed when scoll down on pages. The
I try to make a templated Trie structure. It represents tree in internal private
I'm cleaning up a rotted source tree, and I try to make each executable
in my current project ive a controller tree like this: Controller - ------Admin -
I try to make my ListBox connected to ObservaleCollection be more efficient so for
I try to make an ActiveX by C# with COM-visible. It is a Windows
I try to make an imageview clickable. Actually it is clickable, so that I
I try to make a apps that need barcode scanner, i already can get
I try to make an auto-update feature with my RSS reader application, so it

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.