如何从数组制作目录树

时间:2011-08-31 23:33:01

标签: php

我有一个目录的数组,我不会使用以下代码的目录树...我的问题是如何从这个数组创建一个树?使用此代码,我得到了这个:

Array (
    [0] => store/555555//Osnovni documenti
    [1] => store/555555//Ostali dokumenti
    [2] => store/555555//Pictures )

现在如何补充这个?我可以获得目录树吗?还有什么可以做到的吗?

echo"<pre>";

function make_tree_recursive($path, $levels){
//where $path is your source dir, $levels is the maximum recursives
    $handle=opendir($path);
    while($a=readdir($handle)){
      if(!preg_match('/^\./',$a)){
        $full_path="$path/$a";
        $list[]=$full_path;

        if((is_dir($full_path))&&(!preg_match('/(\/.+){'.$levels.',}/',$full_path))){
          $recursive=make_tree_recursive($full_path, $levels);
          for($n=0; $n<count($recursive); $n++){
              $list[]=$recursive[$n];
          }
        }
      }
    }

    closedir($handle);
    return $list;
}

$list = make_tree_recursive("store/".$diro."/", 3);
print_r ($list);
echo"</pre>";

2 个答案:

答案 0 :(得分:0)

使用array_flip

翻转路径数组并使用库explodeTree

// let '/' be our delimiter
$tree = explodeTree($key_files, "/");
// show the array
print_r($tree);

http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/

答案 1 :(得分:0)

cat <<000 >a0.a
aa/store
aa/store/555555
aa/store/555555/Osnovni documenti
aa/store/555555/Ostali dokumenti
aa/store/555555/Pictures
000

cat a0.a |
    grep -P . |
    while read d0; do echo --- $d0; mkdir -pv $PWD/aa/"$d0"
        done