RecursiveIteratorIterator - 格式化数组

时间:2014-03-13 11:10:02

标签: php arrays

我当前有一个站点显示所有目录和子目录(在MAC OS上) 所以我在这里有代码

$Directory = new RecursiveDirectoryIterator('/');
$Iterator = new RecursiveIteratorIterator($Directory);

foreach($Iterator as $name => $object){
   echo "$name<br/>";
}

所以它将回显目录的所有真实路径(目前正在Windows环境中工作)

C:\xampp\apache\bin
C:\xampp\apache\bin\.
C:\xampp\apache\bin\..
C:\xampp\apache\bin\ab.exe
C:\xampp\apache\bin\abs.exe
C:\myfolder\mysubfolder
C:\myfolder\mysubfolder\.
C:\myfolder\mysubfolder\..
C:\myfolder\mysubfolder\anotherfile.php
C:\myfolder\mysubfolder\another_folder
C:\myfolder\mysubfolder\another_folder\.
C:\myfolder\mysubfolder\another_folder\..
C:\myfolder\mysubfolder\another_folder\file.txt

我想要做的是,在列表中显示 所以像:

C:\
    xampp\
       apache\
           bin\
               ab.exe
               abs.exe
    myfolder\
       mysubfolder\
           anotherfile.php\
           another_folder\
              file.txt

是否可以像上面提到的那样格式化数组中的数据,而不是完整路径? 我不确定如何使用'explode'并将它们分组为新数组......?

我再次尝试这些代码:

foreach($objects as $name => $object){
$newname=explode('\\', $name);
 print_r($newname);
}

它正在给我:

array = { [0] => C:\
[1]=>xampp
[2]=>apache
[3]=>bin}

array = { [0] => C:\ 
[1]=>xampp
[2]=>apache
[3]=>bin 
[4]-> . }

 array = { [0] => C:\ 
[1]=>xampp
[2]=>apache
[3]=>bin 
[4]=>.
[5]=>. . }

    array = { [0] => C:\ 
[1]=>xampp
[2]=>apache
[3]=>bin 
[4]=>ab.exe }

 array = { [0] => C:\ 
[1]=>xampp
[2]=>apache
[3]=>bin 
[4]=>abs.exe }

现在,我不知道如何循环它们以便它们在群组中回响 例如

xampp
  apache
     bin
        ab.exe
        abs.exe

感谢

2 个答案:

答案 0 :(得分:2)

DirectoryIterator(+递归函数)可以为你做到这一点。

$resutl = RecursiveDirectoryIterator(new DirectoryIterator('/path/to/dir'));

function RecursiveDirectoryIterator(DirectoryIterator $path)
{
    $data = array();
    foreach ($path as $node){
        if ($node->isDir() && !$node->isDot()){
            $data[$node->getFilename()] = RecursiveDirectoryIterator(new DirectoryIterator($node->getPathname()));
        }
        elseif ($node->isFile()){
            $data[] = $node->getFilename();
        }
    }
    return $data;
}

答案 1 :(得分:0)

这是非递归版本,它首先对迭代结果进行排序,然后分割路径数量。

<?
$folder = ".";
$Directory = new RecursiveDirectoryIterator($base);
$Iterator = new RecursiveIteratorIterator($Directory);
$SortedIterator = new SortedIterator($Iterator);

foreach($SortedIterator as $name){
    //   echo $name."\n";
   $path = explode(DIRECTORY_SEPARATOR, $name);
   $file = array_pop($path);
   // it's a directory
   if ($file == ".")
   {
      $file = array_pop($path) . DIRECTORY_SEPARATOR;
   }
   if ($file === "..")
   {
     continue;
   }

   $pathcount = count($path);
   echo str_repeat("    ", $pathcount).$file."\n";
}

# Taken from example: http://stackoverflow.com/questions/2930405/sort-directory-listing-using-recursivedirectoryiterator
class SortedIterator extends SplHeap
{
    public function __construct(Iterator $iterator)
    {
        foreach ($iterator as $item) {
            $this->insert($item);
        }
    }
    public function compare($b,$a)
    {
        return strcmp($a->getRealpath(), $b->getRealpath());
    }
}

示例输出:

./
    ChangeLog
    LICENSE.BSD
    README.md
    bin/
        phantomjs
    example.txt
    examples/
        arguments.coffee
        arguments.js
        child_process-examples.coffee
        child_process-examples.js
        colorwheel.coffee
        colorwheel.js
        colorwheel.png
        countdown.coffee
        countdown.js
        detectsniff.coffee
        detectsniff.js
        direction.coffee
        direction.js
        echoToFile.coffee
        echoToFile.js
    rasterize.js
    third-party.txt