phptreeview

How to get folders and their subfolders with their paths?


// data fetched from database, it's dynamic
$treeData = [
    ['id' => 1, 'name' => 'Root A', 'parent_id' => 0],
    ['id' => 2, 'name' => 'Child A', 'parent_id' => 1],
    ['id' => 3, 'name' => 'Grandchild A1', 'parent_id' => 2],
    ['id' => 4, 'name' => 'Grandchild A2', 'parent_id' => 2],
    ['id' => 5, 'name' => 'Child B', 'parent_id' => 1],
    ['id' => 6, 'name' => 'Root B', 'parent_id' => 0],
    ['id' => 7, 'name' => 'Root C', 'parent_id' => 0],
];

// Function to build the hierarchical array from flat data
function buildTree(array &$elements, $parentId = 0) {
    $branch = [];
    foreach ($elements as $element) {
        if ($element['parent_id'] == $parentId) {
            $children = buildTree($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;
            }
            $branch[] = $element;
        }
    }
    return $branch;
}

// Build the hierarchical tree structure
$hierarchicalTree = buildTree($treeData);

i would like to get all the parents and children with their parent path in array as

$output = [
 'Root A',
 'Root A/Child A',
 'Root A/Child A/Grandchild A1',
 'Root A/Child A/Grandchild A2',
 'Root A/Child B',
 'Root B',
 'Root C'
];

I actual task is to create these folders and subfolders dynamically to new users created in my system i was following this answer in stackoverflow


Solution

  • Please try below code:

    <?php
    
    // Original flat data (from DB)
    $treeData = [
        ['id' => 1, 'name' => 'Root A', 'parent_id' => 0],
        ['id' => 2, 'name' => 'Child A', 'parent_id' => 1],
        ['id' => 3, 'name' => 'Grandchild A1', 'parent_id' => 2],
        ['id' => 4, 'name' => 'Grandchild A2', 'parent_id' => 2],
        ['id' => 5, 'name' => 'Child B', 'parent_id' => 1],
        ['id' => 6, 'name' => 'Root B', 'parent_id' => 0],
        ['id' => 7, 'name' => 'Root C', 'parent_id' => 0],
    ];
    
    // Step 1: Build tree structure
    function buildTree(array &$elements, $parentId = 0) {
        $branch = [];
        foreach ($elements as $element) {
            if ($element['parent_id'] == $parentId) {
                $children = buildTree($elements, $element['id']);
                if ($children) {
                    $element['children'] = $children;
                }
                $branch[] = $element;
            }
        }
        return $branch;
    }
    
    // Step 2: Build flat path list
    function buildPaths(array $tree, string $parentPath = ''): array {
        $paths = [];
    
        foreach ($tree as $node) {
            $currentPath = $parentPath === '' ? $node['name'] : $parentPath . '/' . $node['name'];
            $paths[] = $currentPath;
    
            if (isset($node['children'])) {
                $childPaths = buildPaths($node['children'], $currentPath);
                $paths = array_merge($paths, $childPaths);
            }
        }
    
        return $paths;
    }
    
    // Build the hierarchical tree
    $hierarchicalTree = buildTree($treeData);
    
    // Get the output paths
    $output = buildPaths($hierarchicalTree);
    
    // Debug: print the output
    print_r($output);
    

    You can now loop over $output to dynamically create directories for each user, for example:

    foreach ($output as $folderPath) {
        $fullPath = "/users/{$userId}/" . $folderPath;
        if (!is_dir($fullPath)) {
            mkdir($fullPath, 0777, true); // 'true' allows recursive directory creation
        }
    }