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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:23:05+00:00 2026-06-05T00:23:05+00:00

I am trying to create a tree from an array like this: array(‘admin/home’, ‘admin/home/page’,

  • 0

I am trying to create a tree from an array like this:

array(‘admin/home’, ‘admin/home/page’, ‘admin/test’, ‘team/tree/template’, ‘site’);

Or more visually seen as:

admin/home

admin/home/page

admin/test

team/tree/template

site

However, I also need the last nested string of each array to become bolded to signify that it is a webpage itself.

The final product should look like:

    admin
        home
            page
        test
    team
        tree
            template
    site

I have used a few different solutions from this site but I have not been able to get the right outcome.

Any ideas or examples on where I should start would be greatly appreciated, thanks.

  • 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-05T00:23:06+00:00Added an answer on June 5, 2026 at 12:23 am

    You are probably looking for something like this:

    <?php
    
    $items = array('admin/home', 'admin/home/page', 'admin/test', 'site', 'team/tree/template');
    sort($items);
    
    $previous = array();
    foreach ($items as $item) {
        $path = explode('/', $item);
    
        // $i is common nesting level of the previous item
        // e.g. 0 is nothing in common, 1 is one level in common, etc.
        for ($i = 0; $i < min(count($path), count($previous)) && ($path[$i] == $previous[$i]); $i++) { }
    
        // close <ul> from previous nesting levels:
        // that is, we close the amount of levels that the previous and this one have NOT in common
        for ($k = 0; $k < count($previous)-$i-1; $k++) { echo "</ul>"; }
        // $j is the current nesting level
        // we start at the common nesting level
        for ($j = $i; $j < count($path); $j++) {
            // open <ul> for new nesting levels:
            // i.e., we open a level for every level deeper than the previous level
            if ($j >= count($previous))
                echo "<ul>";
            echo "<li>";
            // make the path bold if the end of the current path is reached
            if (count($path)-1 == $j)
                echo "<b>" . $path[$j] . "</b>";
            else
                echo $path[$j];
            echo "</li>";
        }
        $previous = $path;
    }
    // close remaining <ul>
    for ($k = 0; $k < count($previous); $k++) { echo "</ul>"; }
    ?>
    

    Note that I order the array to make sure that all nested items come next to each other.

    It might have some bugs, but I guess you can work them out yourself. Note that I close the list items before opening a nested <ul>. This is not really how it should; I guess you actually should open the nested <ul> in the final <li> at that level, but you can figure that out yourself.

    Update I updated the code to properly close the <ul>‘s in some cases.

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

Sidebar

Related Questions

I am trying to create a tree like node diagram, like the example image
I am trying to create a tree from a source that provides: the 2
I am trying to insert items from my array into my tree. My function
I am trying to create an expression tree. I need to read data from
I'm trying to create a tree from a list of tags which have tags
I'm trying to create an expression tree dynamicly. Let asume that I have two
I have a python script that is trying to create a directory tree dynamically
I'm trying to create a large XML tree in R. Here's a simplified version
I am trying to create a deep copy of my binary tree data structure
I'm trying to create this effect: http://www.thecssninja.com/demo/css_tree/ the problem I'm having is that the

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.