PHP recursiveIteratorIterator:只获取子目录?

时间:2013-09-17 09:56:39

标签: php spl php-5.4

我只想使用RecursiveIteratorIterator

列出主目录中的子目录
$array_directories = array(
    'core/controller/',
    'core/model/',
    'core/helper/'
);

foreach($array_directories as $path_directory){

   $iterator = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($path_directory),
        RecursiveIteratorIterator::SELF_FIRST
    );

    foreach ($iterator as $fileObject) {
        if ($fileObject->isDir()) {
            $files[] = $fileObject;
        }
    }


}

print_r($files);

结果,

Array
(
    [0] => SplFileInfo Object
        (
            [pathName:SplFileInfo:private] => core/controller\webfront
            [fileName:SplFileInfo:private] => webfront
        )

    [1] => SplFileInfo Object
        (
            [pathName:SplFileInfo:private] => core/model\design
            [fileName:SplFileInfo:private] => design
        )

)

但我在追求,

Array
(
    [0] => core/controller\webfront
    [1] => core/model\design
)

有可能吗?

1 个答案:

答案 0 :(得分:1)

if ($fileObject->isDir()) {
    $files[] = $fileObject->getPathname();
}