PHP - Split string and form it as an array with parent child relation
12:20 08 Jan 2022

Edit: @Pontus Carme solution solved this question.


Lets say that I have multiple strings of files directory and files... (Folders structure is dynamic, it shouldn't follow static pattern)

Reference how folder structure looks:
- Root folder: $files = 'File1.doc, File2.doc';
- Subfolder: $files = 'Subfolder/File1.doc';
- Mixed: $files = 'File1.doc, 
 Subfolder/File1.doc, 
 Subfolder/Subsubfolder/File1.doc, 
 File2.doc';
 etc.
---------
Back to code:
$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';

// Make it as an array
$files = explode(',', $files);

...First I want to split every file string and extract directories and file

$data = [];
foreach($files as $key => $file)
{
    $separator = explode('/', $file); // Output: [0 => '2021', 1 => 'Dec', 2 => 'File1.doc'];
    
    $file_name = end($separator); // Get file name as last element
    array_pop($separator); // Remove file name from array
    
    $files = [];
    
    $files[] = [
        'name' => $file_name,
        'folder' => $separator,
        ];
    $node = [];
    
    foreach ($files as $row)
    {
        $node['id'] = $key;
        // This is main folder (root)
        if (empty($row['folder']))
        {
            $node['file'] = $row['name'];
            $node['parent_id'] = 0;
        }
        else
        {
            // THIS IS PROBLEMATIC HOW TO NOW GET PARENT ID AND FOLDER NAME
            foreach ($row['folder'] as $fkey => $frow)
            {
                // I NEED HERE A PARENT_ID FROM PREVIOUS CHILD AND FILE NAME OF CURRENT FOLDER
            }
        }
    }
    $data[] = $node;
}

... Now I need to get folder names and check if folder has subfolder and set parent_id = id of folder.

...How to produce this array structure from the foreach loop above?

[
    [
        'id' => 1,
        'file' => '2021',
        'parent_id' => 0, // Main folder (2021) (NOT ROOT, just first folder generated in directory)
    ],
    [
        'id' => 2,
        'file' => 'Dec',
        'parent_id' => 1, // Child of 2021
    ],
    [
        'id' => 3,
        'file' => 'File1.doc',
        'parent_id' => 2, // Child of Dec
    ],
    [
        'id' => 4,
        'file' => 'File2.doc',
        'parent_id' => 2, // Child of Dec
    ],
    [
        'id' => 5,
        'file' => 'File3.doc',
        'parent_id' => 2, // Child of Dec
    ],
    [
        'id' => 6,
        'file' => 'Nov',
        'parent_id' => 1, // Child of 2021
    ],
    [
        'id' => 7,
        'file' => 'File1.doc',
        'parent_id' => 6, // Child of Nov
    ],
    [
        'id' => 8,
        'file' => 'File2.doc',
        'parent_id' => 6, // Child of Nov
    ],
    [
        'id' => 9,
        'file' => 'Fil3.doc',
        'parent_id' => 6, // Child of Nov
    ],
    [
        'id' => 10,
        'file' => 'File4.doc',
        'parent_id' => 6, // Child of Nov
    ],
    [
        'id' => 11,
        'file' => '2020',
        'parent_id' => 0, // Main folder (2020) (NOT ROOT, just second main folder generated in directory)
    ],
    [
        'id' => 12,
        'file' => 'Jan',
        'parent_id' => 11, // Child of 2020
    ],
    [
        'id' => 13,
        'file' => 'File1.doc',
        'parent_id' => 11, // Child of jan
    ],
    [
        'id' => 14,
        'file' => 'File2.doc',
        'parent_id' => 11, // Child of jan
    ],
    [
        'id' => 15,
        'file' => 'File3.doc',
        'parent_id' => 11, // Child of jan
    ],
];

Here is how final structure needs to looks like and how I plan to use recursive function to get tree-level array.

https://3v4l.org/6ULBZ

php arrays string recursion