【问题标题】:Multidimensional Directory List with Recursive iterator具有递归迭代器的多维目录列表
【发布时间】:2014-07-30 01:53:15
【问题描述】:

我正在尝试获取格式如下的目录的多维数组:

[
    {
        "text": "another_folder",
        "href": "gui\/default\/uploads\/another_folder",
        "depth": 0
    },
    {
        "text": "subfold",
        "href": "gui\/default\/uploads\/subfold",
        "depth": 0,
        "nodes": {
                   "text": "sub-subfold",
                   "href": "gui\/default\/uploads\/subfold\/sub-subfold",
                   "depth": 1,
                  }
    }
]

我想使用 RecursiveIterators。到目前为止,我所做的是获取给定路径中列出的所有目录。我需要进入孩子们那里,这是我堆叠的地方。

public function list_folders($folder_path='') {

    if(!$folder_path) $folder_path = $this->upl_path;

    $iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($folder_path),
                RecursiveIteratorIterator::SELF_FIRST);
    $iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
    $r = array();
    $counter = 0
    foreach ($iterator as $splFileInfo) {
      if($splFileInfo->isDir()) {
        $r[$counter] = array(
          'text' => $splFileInfo->getFilename(),
          'href' => str_replace('\\','/',$splFileInfo->getPathname())
        );
        if(How to check if it has children) {
          $result[$counter] += array('nodes'=> CALL RECURSIVE HERE ? );
      }
      $counter++;
    }


    echo json_encode($r,JSON_PRETTY_PRINT);
}

我很乐意使用任何想法或帮助。

【问题讨论】:

    标签: php file recursion multidimensional-array iterator


    【解决方案1】:

    您的代码几乎可以正常运行,但缺少一些关键点。我调整了您的代码以使其正常工作,并添加了一些 cmets,希望能帮助您了解您所缺少的内容:

    class FolderListing
    {
    
        public function list_folders($folder_path = '', $depth = 0)
        {
    
            if (!$folder_path) $folder_path = $this->upl_path;
    
            $iterator = new IteratorIterator(new DirectoryIterator($folder_path));
    
            $r = array();
            foreach ($iterator as $splFileInfo) {
    
                if ($splFileInfo->isDot()) {
                    continue;
                }
    
                // we need to do this for both folders and files
                $info = array(
                    'text' => $splFileInfo->getFilename(),
                    'href' => str_replace('\\', '/', $splFileInfo->getPathname()),
                    'depth' => $depth
                );
    
                // is we have a directory, try and get its children
                if ($splFileInfo->isDir()) {
                    // !!! I recommend to do an echo $splFileInfo->getPathname() here
                    // to see the order in which recursion works !!!
                    $nodes = $this->list_folders($splFileInfo->getPathname(), $depth + 1);
    
                    // only add the nodes if we have some
                    if (!empty($nodes)) {
                        $info['nodes'] = $nodes;
                    }
                }
                // add the info to the array. No need for a counter :)
                $r[] = $info;
            }
    
            // the return is important to be able to build the multi dimensional array
            return $r;
        }
    }
    
    $test = new FolderListing();
    $ret = $test->list_folders('./test'); // change this to whatever you want
    var_dump($ret);
    

    祝你好运!

    【讨论】:

    • 非常感谢您的回复。一个松散的支架。它正确创建多维数组,但例如子子文件夹显示两次。首先它正确定位在正确的数组级别。其次是在最后的父级。我们如何告诉脚本,“我们使用了它,将它从您的显示器中删除?”
    • 这里是 json 输出。在 line:49 之后,它是重复的。 pastebin.com/xiERcQBY
    • 啊,当然 :) 这是因为已经使用了递归迭代器,我为每个子文件夹再次运行它。所以我停止使用 RecursiveDirectoryIterator 并且不再存在重复项,我还将尝试使用 RecursiveIterator 制作一个版本,看看它是如何工作的。即将推出:)
    • 顺便更新了代码,现在可以正常使用new IteratorIterator(new DirectoryIterator($folder_path));
    • RecursiveIterator 应该有很好的机会,有时间我会尝试做的:)
    猜你喜欢
    • 2012-11-06
    • 1970-01-01
    • 2013-12-01
    • 2020-10-09
    • 2013-11-11
    • 1970-01-01
    • 2020-03-21
    • 2021-02-09
    相关资源
    最近更新 更多