用php制作treeview

用php制作treeview,php,file,recursion,treeview,Php,File,Recursion,Treeview,我有几个数组,如下所示: $files = Array ('dir1/dir2/dir3/file1', 'dir1/dir2/dir3/file2', 'dir1/dir2/dir3/file3', 'dir1/dir2/dir3/file4' ); 我徒劳地尝试了几个递归函数进行如下转换: $files = Array ( 'dir1' => Array (

我有几个数组,如下所示:

$files = Array ('dir1/dir2/dir3/file1', 
            'dir1/dir2/dir3/file2', 
            'dir1/dir2/dir3/file3',
            'dir1/dir2/dir3/file4'
        );
我徒劳地尝试了几个递归函数进行如下转换:

$files = Array (
        'dir1' => Array (
                'dir2' => Array (
                            'dir3' => Array (
                                        'file1', 
                                        'file2', 
                                        'file3', 
                                        'file4'
                             )
                 )
         )
);
$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);
请帮帮我你的意思是这样的:

$files = Array (
        'dir1' => Array (
                'dir2' => Array (
                            'dir3' => Array (
                                        'file1', 
                                        'file2', 
                                        'file3', 
                                        'file4'
                             )
                 )
         )
);
$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);
$files=array('dir1/dir2/dir3/file1',
“dir1/dir2/dir3/file2”,
“dir1/dir2/dir3/file3”,
“dir1/dir2/dir3/file4”
);
$newFiles=array();
foreach($files作为$file){
$one=explode('/',$file);//explode'/'获取每个值
$last=array_pop($one);//弹出最后一项,因为它是文件
$rev=array_reverse($one);//我们反转数组以便将最后一个附加到上一个
$mixArray=array();//创建一个临时数组
foreach($revas$num=>$dir){//在反向数组中循环以提取目录
$mixArray[$dir]=$last;//将最后一项附加到当前目录,第一个循环将文件放入最后一个目录
$last=$mixArray;//用当前创建的数组覆盖最后一个变量
如果($num
你的意思是这样的:

$files = Array (
        'dir1' => Array (
                'dir2' => Array (
                            'dir3' => Array (
                                        'file1', 
                                        'file2', 
                                        'file3', 
                                        'file4'
                             )
                 )
         )
);
$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);
$files=array('dir1/dir2/dir3/file1',
“dir1/dir2/dir3/file2”,
“dir1/dir2/dir3/file3”,
“dir1/dir2/dir3/file4”
);
$newFiles=array();
foreach($files作为$file){
$one=explode('/',$file);//explode'/'获取每个值
$last=array_pop($one);//弹出最后一项,因为它是文件
$rev=array_reverse($one);//我们反转数组以便将最后一个附加到上一个
$mixArray=array();//创建一个临时数组
foreach($revas$num=>$dir){//在反向数组中循环以提取目录
$mixArray[$dir]=$last;//将最后一项附加到当前目录,第一个循环将文件放入最后一个目录
$last=$mixArray;//用当前创建的数组覆盖最后一个变量
如果($num


您尝试的功能遇到了哪些问题?你在设计一个可以做到这一点的算法时遇到了什么问题?请告诉我们你已经尝试了什么-我们将尝试为你指出正确的方向。我创建的函数不是递归的,也不好看,它是90多个在线代码。如果你要寻求帮助,你需要展示一些努力。如果你拒绝展示你的努力,这会让人们认为你自己没有尝试过任何东西。你尝试过的功能遇到了什么问题?你在设计一个可以做到这一点的算法时遇到了什么问题?请告诉我们你已经尝试了什么-我们将尝试为你指出正确的方向。我创建的函数不是递归的,也不好看,它是90多个在线代码。如果你要寻求帮助,你需要展示一些努力。如果你拒绝展示你的努力,这会让人们认为你自己什么都没试过。非常感谢你,你救了我的命,非常感谢:)一个附带的问题,你明白我在那里做了什么吗?是的,接近60%,一旦我仔细分析了你的代码,我就应该接受你的逻辑这里是我使用的函数:它工作得很好,但代码和性能水平不是很干净。你真的很好,我不知道该说什么,尊敬:)非常感谢你,你救了我的命,非常感谢:)一个附带问题,你了解我在那里所做的事情吗?是的,接近60%,我应该在仔细分析你的代码后接受你的逻辑这里是我使用的函数:它工作完美,但不是非常干净的代码和性能级别。你真是太好了,我不知道说什么,尊敬:)