Jump to content

Read Directory Structure Into Array


flyhoney

Recommended Posts

This is really more of an algorithm question, but I'm doing it in PHP and maybe some of you out there just love problem solving.  I want to read a list of filenames into an array that reflects the directory structure.  The list looks something like this:

 

C:\this\file1.ext
C:\this\file2.ext
C:\this\that\file3.ext
C:\foo\file6.ext
.
.
etc

 

And I want to read it into an array so that it looks like this:

Array
(
    [C:] => Array
        (
            [this] => Array
                (
                    [0] => file1.ext
                    [1] => file2.ext
                    [that] => Array
                        (
                            [0] => file3.ext
                        )

                )

            [foo] => Array
                (
                    [0] => file6.ext
                )

        )

)

 

I know there is probably a nice recursive solution to this, but I keep screwing it up.  If anyone knows how to do this off the top of their head, it would be a great help.  If you can think of a better data structure to store the directory information in just let me know.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.