$out = [
'dir1' => [
'sdir1' => ['file'],
'sdir2' => ['sdir3' => ['file']],
],
'dir2' => [
'sdir1' =>['file']
]
]
// Функция для превращения путей в древовидный массив
function pathsToTree(array $paths, $separator="/") {
$pathsTree = [];
foreach ($paths as $path) {
$pathParts = array_reverse(explode($separator, $path));
$pathTree = [];
foreach ($pathParts as $i=>$pathPart) {
if (empty($pathPart)) {
continue;
}
$pathTree = ($i == 0 ? [$pathPart] : [$pathPart => $pathTree]);
}
$pathsTree = array_merge_recursive($pathsTree, $pathTree);
}
return $pathsTree;
}
// Пример использования
$testPaths = [
'/dir1/sdir1/file',
'/dir1/sdir2/sdir3/file',
'/dir2/sdir1/file'
];
var_dump(pathsToTree($testPaths));
/* Результат:
array(2) {
["dir1"]=>
array(2) {
["sdir1"]=>
array(1) {
[0]=>
string(4) "file"
}
["sdir2"]=>
array(1) {
["sdir3"]=>
array(1) {
[0]=>
string(4) "file"
}
}
}
["dir2"]=>
array(1) {
["sdir1"]=>
array(1) {
[0]=>
string(4) "file"
}
}
}
*/