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
https://forums.phpfreaks.com/topic/90726-read-directory-structure-into-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.