0

For example, I have array

Array
(
    [0] => folder1/file1.txt
    [1] => folder1/file2.txt
    [2] => file2.txt
    [3] => folder2/file1.txt
    [4] => folder1/subfolder1/file1.txt
    [5] => folder1/subfolder2/file2.txt
    [6] => file1.txt
    [7] => file3.txt
    [8] => folder1/subfolder2/file1.txt
)

I need a clue to figure how to create 'directory tree' array, based on the given values, that it could look like this:

Array
(
    [folder1] => Array
        (
            [0] => file1.txt
            [1] => file2.txt
            [subfolder1] => Array
                (
                    [0] => file1.txt
                )

            [subfolder2] => Array
                (
                    [0] => file1.txt
                    [1] => file2.txt
                )

        )

    [0] => file1.txt
    [1] => file2.txt
    [2] => file3.txt
)

Now second array is a tree of the first array (made manually). =)

And I can't figure how to achieve this automatically.

12
  • Possible dupe of http://stackoverflow.com/questions/8479543/php-how-to-populate-a-directory-structure-in-an-array? Commented Oct 2, 2013 at 16:36
  • @Kristen Jukowski that topic involves real files and dirs, and I don't have ones Commented Oct 2, 2013 at 16:41
  • 1
    @KristenJukowski That question starts with a recursive directory iterator, this question starts with a flat array. I'm pretty sure I answered a similar question a while back, I'm trying to find it. Commented Oct 2, 2013 at 16:41
  • @Barmar I've searched for it too. unsuccessfully Commented Oct 2, 2013 at 16:42
  • Something like this? But that kind of structure calls for trouble: what if you have folder named 0 ? Commented Oct 2, 2013 at 16:56

1 Answer 1

1

Simples example (demo), that will generate output like you wish, but you will get conflict if you will have same folder and file name in the same directory level (demo).

$files = [
    'folder1/file1.txt',
    'folder1/file2.txt',
    'file2.txt',
    'folder2/file1.txt',
    'folder1/subfolder1/file1.txt',
    'folder1/subfolder2/file2.txt',
    'file1.txt',
    'file3.txt',
    'folder1/subfolder2/file1.txt',
];

$tree = [];
foreach ($files as $file) {
    $a = explode('/', $file);
    $array = &$tree;
    foreach (array_slice($a, 0, -1) as $folder) {
        if (!isset($array[$folder])) $array[$folder] = [];
        $array = &$array[$folder];
    }
    $array[] = end($a);
}
print_r($tree);

To overcome problem with same folder and file named on the same level you can create folder indexes with slash at the end, like folder/, so there cannot be any conflict (demo).

Sign up to request clarification or add additional context in comments.

2 Comments

you can create folder indexes do you mean empty folders?
No, I meant that if you have a folder, array index will be with slash as folder/. And empty folders are also supported. Example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.