<?php

$paths = [
    'www/webs/apps/pics',
    'www/webs/test',
];

$finalArray = [];
foreach($paths as $path){
    $revArr = array_reverse(explode("/", $path)); // This is the trick. Start from build inside out.
    $pathArray = [];
    foreach($revArr as $key){
            $tempBucket = $pathArray;
            $pathArray = []; // Create fresh empty array to hold the 
            $pathArray[$key] = $tempBucket;
    }
    
    $finalArray = array_merge_recursive($pathArray, $finalArray); // Recursively Merge the array into the main array
}

echo "<pre>";
print_r(json_encode($finalArray, JSON_PRETTY_PRINT));
echo "</pre>";
