具有递归迭代器的多维目录列表 [英] Multidimensional Directory List with Recursive iterator

查看:106
本文介绍了具有递归迭代器的多维目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get multi-dimensional array for directories formatted as below :

[
    {
        "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。到目前为止我所做的是我在给定路径中列出所有目录。我需要进入我堆积的孩子。

I want to use RecursiveIterators. What I have done so far is I am getting all directories listed in given path. I need to go inside to children which is where I stacked.

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);
}

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

I'd use any idea or help gladly.

推荐答案

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

Your code was almost functional, but it was missing a few key points. I adapted your code so that it works, and added some comments that I hope will help you understand what you were missing:

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);

祝你好运!

这篇关于具有递归迭代器的多维目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆