【问题标题】:PHP - create dynamic multidimensional file tree arrayPHP - 创建动态多维文件树数组
【发布时间】:2013-10-24 17:57:20
【问题描述】:

我想创建一个文件树,为此我需要将一个文件和目录数组转换为一个多维文件树数组。例如:

array
(
   'file.txt',
   'dir1/',
   'dir1/dir2/',
   'dir1/dir2/dir3/',
   'dir1/file.txt',
)

array
(
   'file.txt',
   'dir1' => 
   array
   (
       'dir2' => 
       array
       (
           'dir3' =>
           array(),
       ),
       'file.txt',
    )
)

我已经尝试了几个函数来完成这个,但它们都不起作用。例如,我遇到的问题是没有简单的方法将array ('test','test','test'),'test' 转换为$array['test']['test']['test'] = 'test'

【问题讨论】:

    标签: php arrays multidimensional-array tree filesystems


    【解决方案1】:

    这是一个较短的递归:

    function dir_tree($dir) {    
        $files = array_map('basename', glob("$dir/*"));
        foreach($files as $file) {
            if(is_dir("$dir/$file")) {
                $return[$file] = dir_tree("$dir/$file");
            } else {
                $return[] = $file;
            }
        }
        return $return;
    }
    

    【讨论】:

    • 一直在寻找这样的东西。我很高兴在重新发明轮子之前看到了这个。
    【解决方案2】:

    看看my post here

    答案是:strtok 会救你。

    <?php
    
    $input = [
    '/RootFolder/Folder1/File1.doc',
    '/RootFolder/Folder1/SubFolder1/File1.txt',
    '/RootFolder/Folder1/SubFolder1/File2.txt',
    '/RootFolder/Folder2/SubFolder1/File2.txt',
    '/RootFolder/Folder2/SubFolder1/SubSubFolder1/File4.doc',
    ];
    
    function parseInput($input) {
      $result = array();
    
      foreach ($input AS $path) {
      $prev = &$result;
    
      $s = strtok($path, '/');
    
      while (($next = strtok('/')) !== false) {
        if (!isset($prev[$s])) {
          $prev[$s] = array();
        }
    
      $prev = &$prev[$s];
      $s = $next;
      }
    $prev[] = $s;
    
    unset($prev);
    }
    return $result;
    }
    
    var_dump(parseInput($input));
    

    输出:

    array(1) {
      ["RootFolder"]=>
      array(2) {
        ["Folder1"]=>
         array(2) {
           [0]=>
           string(9) "File1.doc"
           ["SubFolder1"]=>
           array(2) {
             [0]=>
        string(9) "File1.txt"
             [1]=>
             string(9) "File2.txt"
           }
         }
         ["Folder2"]=>
         array(1) {
           ["SubFolder1"]=>
           array(2) {
             [0]=>
             string(9) "File2.txt"
             ["SubSubFolder1"]=>
             array(1) {
               [0]=>
               string(9) "File4.doc"
             }
           }
         }
       }
     }
    

    【讨论】:

    • 完美,整天绞尽脑汁,就是这样!超级棒!
    【解决方案3】:

    为此我有 PHP sn-p:

    <?php
    function wps_glob($dir) {
      foreach (glob($dir . '/*') as $f) {
        if(is_dir($f)) {
          $r[] = array(basename($f) => wps_glob($f));
        }
        else {
          $r[] = basename($f);
        }
      }
      return $r;
    }
    
    function wps_files($path) {
      $wpsdir = Array(
         'root' => $path,
         'struktur' =>  wps_glob($path)
      );
      return $wpsdir;
    }
    ?>
    

    example usage here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多