文件目录列表如何转成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 k e y = > key=> key=>file){
p a t h = r t r i m ( path = rtrim( path=rtrim(file, ‘/’); //如果是目录,去掉目录后面的斜杠
$arr = explode(‘/’, $path); //把str路径转数组
$parent = ‘’; //定义动态路径,放下找时的临时存放路径
foreach($arr as $item){ //从顶级一级一级往下找
$pid = $parent == ‘’ ? 0 : d a t a [ data[ data[parent][‘id’]; //查找父ID
$parent .= $parent == ‘’ ? $item : ‘/’ . $item; //拼接路径
if(isset( d a t a [ data[ data[parent])) //如果已存在,跳过
continue;
d a t a [ data[ data[parent] = array(
‘id’ => $key+1, //ID
‘pid’ => $pid, //父ID
‘level’ => substr_count($parent, ‘/’) + 1, //层级
‘name’ => i t e m . ( s u b s t r ( item . (substr( 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) {
l i s t = a r r a y c o l u m n ( list = array_column( list=arraycolumn(list, null, ‘id’);
foreach ($list as $key => $val) {
if ($val[‘pid’]) {
if (isset( l i s t [ list[ list[val[‘pid’]])) {
if ($useKey) {
l i s t [ list[ list[val[‘pid’]][‘children’][KaTeX parse error: Expected 'EOF', got '&' at position 8: key] = &̲list[$key];
} else {
l i s t [ list[ list[val[‘pid’]][‘children’][] = & l i s t [ list[ list[key];
}
}
}
}
foreach ($list as $key => $val) {
if ( v a l [ ′ p i d ′ ] ) u n s e t ( val['pid']) unset( 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 = z i p − > s t a t I n d e x ( zip->statIndex( 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($list)));
结果输出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
)
)
)
)
)
)
完成!