1 Answer
1

$files = array('dir1/dir2/dir3/file1',
'dir1/dir2/dir3/file2',
'dir1/dir2/dir3/file3',
'dir1/dir2/dir3/file4'
);
$newFiles = array();
foreach($files as $file){
$one = explode('/', $file); // explode '/' to get each value
$last = array_pop($one); // pop the last item because it is the file
$rev = array_reverse($one); // we reverse the array in order to append the last to previous
$mixArray = array(); // create a temporary array
foreach($rev as $num => $dir){ // loop in reversed array to extract directories
$mixArray[$dir] = $last; // append the last item to the current dir, the first loop puts the file to the last directory
$last = $mixArray; // overwrite last variable with current created array
if($num < count($rev)-1){
unset($mixArray); // if the current directory is not the last in reversed array we unset it because we will have duplicates
}
}
$newFiles = array_merge_recursive($newFiles, $mixArray); // merge recursive the result to main array
}
var_export($newFiles);