php文件目录列表转成tree状

知兮丶青 函数 · tree
阅读(482) 2021-02-07
php文件目录列表转成tree状
php文件目录列表转成tree状

文件目录列表如何转成tree状,即格式化文件目录列表,获得具有父子级关系数据后,再转成tree装数据结构。


目录列表转父子关系数据

/**
 * 格式化文件目录列表,使得数据具有父子级关系
 * @param array $files 文件目录列表,如:['a/','a/b/','a/b/c.txt','a/c/','b']
 * @return array 父子级关系数组
 */
function format_file_list($files){
    $data = []; //数组用来存放,格式化后的数据
    sort($files); //对文件目录列表排序
    foreach($files as $key=>$file){
        $path = rtrim($file, '/'); //如果是目录,去掉目录后面的斜杠
        $arr = explode('/', $path); //把str路径转数组
        $parent = ''; //定义动态路径,放下找时的临时存放路径
        foreach($arr as $item){ //从顶级一级一级往下找
            $pid = $parent == '' ? 0 : $data[$parent]['id']; //查找父ID
            $parent .= $parent == '' ? $item : '/' . $item; //拼接路径
            if(isset($data[$parent]))  //如果已存在,跳过
                continue;
            $data[$parent] = array(
                'id' => $key+1, //ID
                'pid' => $pid, //父ID
                'level' => substr_count($parent, '/') + 1, //层级
                'name' => $item . (substr($file, -1) === '/' ? '/' : ''), //名称,如果是目录保留后面斜杠
                'path' => $parent, //目录
            );
        }
    }
    return $data;
}

父子关系数据转tree

/**
 * 列表转树形(迭代)
 * @param array $list
 * @param bool $useKey 是否使用ID作为键值
 * @return array
 */
function list_to_tree($list, $useKey = false) {
    $list = array_column($list, null, 'id');
    foreach ($list as $key => $val) {
        if ($val['pid']) {
            if (isset($list[$val['pid']])) {
                if ($useKey) {
                    $list[$val['pid']]['children'][$key] = &$list[$key];
                } else {
                    $list[$val['pid']]['children'][] = &$list[$key];
                }
            }
        }
    }
    foreach ($list as $key => $val) {
        if ($val['pid']) unset($list[$key]);
    }
    if ($useKey) {
        return $list;
    } else {
        return array_values($list);
    }
}

用例测试

//首先读取一个压缩包目录列表,作为测试初始数据
$zip = new ZipArchive;
$zip->open('C:\\Users\\XQ\\Desktop\\bootstrap-4.4.1-dist.zip', ZipArchive::CREATE);
$list = [];
for($i = 0; $inumFiles; $i++){
    $stat = $zip->statIndex($i);
    $list[] = iconv('GBK//IGNORE', 'UTF-8', $stat['name']);
}
//得到:
//Array(
//    [0] => bootstrap-4.4.1-dist/
//    [1] => bootstrap-4.4.1-dist/js/
//    [2] => bootstrap-4.4.1-dist/js/bootstrap.min.js.map
//    [3] => bootstrap-4.4.1-dist/js/bootstrap.min.js
//    [4] => bootstrap-4.4.1-dist/css/
//    [5] => bootstrap-4.4.1-dist/css/bootstrap.min.css
//    [6] => bootstrap-4.4.1-dist/css/bootstrap.min.css.map
//)

print_r(list_to_tree(format_file_list($html)));

结果输出

Array(
    [0] => Array
            [id] => 1
            [pid] => 0
            [level] => 1
            [name] => bootstrap-4.4.1-dist/
            [path] => bootstrap-4.4.1-dist
            [children] => Array(
                    [0] => Array(
                            [id] => 2
                            [pid] => 1
                            [level] => 2
                            [name] => css/
                            [path] => bootstrap-4.4.1-dist/css
                            [children] => Array(
                                    [0] => Array(
                                            [id] => 3
                                            [pid] => 2
                                            [level] => 3
                                            [name] => bootstrap.min.css
                                            [path] => bootstrap-4.4.1-dist/css/bootstrap.min.css
                                        )
                                    [1] => Array(
                                            [id] => 4
                                            [pid] => 2
                                            [level] => 3
                                            [name] => bootstrap.min.css.map
                                            [path] => bootstrap-4.4.1-dist/css/bootstrap.min.css.map
                                        )
                                )
                        )
                    [1] => Array(
                            [id] => 5
                            [pid] => 1
                            [level] => 2
                            [name] => js/
                            [path] => bootstrap-4.4.1-dist/js
                            [children] => Array(
                                    [0] => Array(
                                            [id] => 6
                                            [pid] => 5
                                            [level] => 3
                                            [name] => bootstrap.min.js
                                            [path] => bootstrap-4.4.1-dist/js/bootstrap.min.js
                                        )
                                    [1] => Array(
                                            [id] => 7
                                            [pid] => 5
                                            [level] => 3
                                            [name] => bootstrap.min.js.map
                                            [path] => bootstrap-4.4.1-dist/js/bootstrap.min.js.map
                                        )
                                )
                        )
                )
        )
)

完成!




原创文章,转载请注明出处:https://www.weizhixi.com/article/112.html