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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:49:38+00:00 2026-06-06T13:49:38+00:00

I have an array of file path components like this: [ [‘some’, ‘dir’, ‘file.txt’],

  • 0

I have an array of file path components like this:

[ ['some', 'dir', 'file.txt'],
  ['other', 'folder', 'here.txt'],
  ['this', 'one', 'is', 'deeper', 'file.txt'],
  ['some', 'dir', 'second.txt'
]

So the array contains arrays each consisting of path components to a file. The last element in an inner array is always the file itself, with the preceding elements being directories leading to the file.

What I’m trying to figure out is how to transform the above data so that I can easily generate a file tree with it using <ul> and <li> tags such that folders are nested within each other and files within the same folder show up together. All sorted alphabetically.

From the above I would like to generate the following. The file <li> themselves have to be links to the path to that file:

<ul>
  <li>some/
    <ul>
      <li>dir/
        <ul>
          <li><a href="some/dir/file.txt">file.txt</a></li>
          <li><a href="some/dir/second.txt">second.txt</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li>other/
    <ul>
      <li>folder/
        <ul>
          <li><a href="other/folder/here.txt">here.txt<a/></li>
        </ul>
      </li>
    </ul>
  </li>
  <li>this/
    <ul>
      <li>one/
        <ul>
          <li>is/
            <ul>
              <li>deeper/
                <ul>
                  <li><a href="this/one/is/deeper/file.txt">file.txt</a></li>
                </ul>
              </li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

Thanks, I’d appreciate any ideas.

  • 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-06T13:49:41+00:00Added an answer on June 6, 2026 at 1:49 pm

    Rough outline; as simple as possible (i.e., no tricks to keep things simple 🙂

    require 'pp'
    
    dir = {}
    
    files = [
      ['some', 'dir', 'file.txt'],
      ['other', 'folder', 'here.txt'],
      ['this', 'one', 'is', 'deeper', 'file.txt'],
      ['some', 'dir', 'second.txt']
    ]
    
    def merge_paths(h, paths)
      top = paths[0]
    
      if paths.size == 1
        h[top] = top
      else
        h[top] ||= {}
        merge_paths h[top], paths[1..-1]
      end
    end
    
    files.each do |paths|
      merge_paths dir, paths
    end
    
    pp dir
    

    Outputs:

    {"some"=>{"dir"=>{"file.txt"=>"file.txt", "second.txt"=>"second.txt"}},
     "other"=>{"folder"=>{"here.txt"=>"here.txt"}},
     "this"=>{"one"=>{"is"=>{"deeper"=>{"file.txt"=>"file.txt"}}}}}
    

    Creating the lists is essentially the same process; recurse over hash keys. You’re at the last level when a hash value isn’t another hash. You might want to also sort by name and/or type, e.g., put directories (key values that are hashes) first, and so on.

    There are a number of games you can play with this, including turning it into just a few lines of code, combined with gems like deep_merge to reduce the amount of busy-work you have to do manually.

    This also doesn’t do any “sanity checking” to make sure the data isn’t pathological, e.g., you could construct an array that would turn a filename into a directory, wipe out a directory with a filename, and so on–an exercise left for the reader.

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

Sidebar

Related Questions

I have a string array of some file paths: path/to/folder/file.xxx path/to/other/ path/to/file/file.xx path/file.x path/
I have an array of file-path strings like this videos/funny/jelloman.wmv videos/funny/bellydance.flv videos/abc.mp4 videos/june.mp4 videos/cleaver.mp4
I'm trying to make an array from file path components. I have an array
I have defined file path like- $docRoot = $_SERVER['DOCUMENT_ROOT']; $webRoot = str_replace(array($docRoot, 'library/config.php'), '',
I have an array of file names in Powershell, and I would like to
well i have an array which takes it's data from a plist file like
I have data contained in an array which is like so, $file['info']['files'] = array(
I have an array of file names which I want to download. The array
I have a array stored in a PHP file in which I am storing
In my main file I have an array of character strings, char names[320][30], and

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.