简体   繁体   中英

PHP - Create multidimensional recursive array from string (Files/folders structure)

Someone asked same question 10 years ago but there is no answer to that

Reference: Trying to create Array from String (file/folder structure)

I want to return a tree-level multidimensional array from file list stored in database as a string.

For example I store my work related path in database like this:

SELECT name FROM files;

... SQL Output

2021/Dec/File1.doc
2021/Dec/File2.doc
2021/Dec/File3.doc
2021/Nov/File1.doc
2021/Nov/File2.doc
2021/Nov/File3.doc
2021/Nov/File4.doc
2020/Jan/File1.doc
2020/Jan/File2.doc
2020/Jan/File3.doc

... PHP recursive array output should be categorized by folders, subfolders, more subfolders if exists and files on end.

-2021
--Dec
---File1.doc
---File2.doc
---File3.doc
--Nov
---File1.doc
---File2.doc
---File3.doc
---File4.doc
-2020
--Jan
---File1.doc
---File2.doc
---File3.doc

What would be the best way to achieve this performance wise?

What I got so far...

$files = [];
foreach ($sql as $row)
{
    // Separate directories
    $separator = explode('/', $row['name']); 
    /* Output:
    Array
    (
    [0] => 2021
    [1] => Dec
    [2] => file1.doc
    )
    */

    // Find the file via regex
    if (preg_match('/[^\/]*\.(doc|txt)/', $row['name'], $o))
    {
        $row['name'] = $o[0]; //Output: file1.doc
    }
    $files[] = $row;
}

... For now I only have a file names, now I need directories as well and then make a multidimensional array from it.

I have one solution for you, I hope this will help.

$files="2021/Dec/File1.doc, 
2021/Dec/File2.doc,
2021/Dec/File3.doc,
2021/Nov/File1.doc,
2021/Nov/File2.doc,
2021/Nov/File3.doc,
2021/Nov/File4.doc,
2020/Jan/File1.doc,
2020/Jan/File2.doc,
2020/Jan/File3.doc";

$files=explode(',',$files);

foreach($files as $file)
{
    $i=1;
    $paths=explode('/',$file);
    foreach($paths as $path)
    {
        for($j=0;$j<$i;++$j){
            echo '-';
        }
        echo $path;
        echo "<br/>";
        ++$i;
    }
    $i=0;
}

You can use your database files. by removing my files constant value and use your.

$files = []; foreach ($sql as $row) { // Separate directories $separator = explode('/', $row['name']); /* Output: Array ( [0] => 2021 [1] => Dec [2] => file1.doc ) */ if (preg_match('/[^\/]*\.(doc|txt)/', $row['name'])) { $node = &$files; while (count($separator) > 1) { $folder = array_shift($separator); if (!array_key_exists($folder, $node)) { $node[$folder] = []; } $node = &$node[$folder]; } $node[] = $separator[0]; } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM