如何获取目录中第一个子目录名称的列表?php - 获取第一个子目录名称列表
下面是一个例子树:
dir/
dir_first a/
dir second aa
dir second ab
dir second ac
dir_first b/
dir second ba
dir second bb
dir second bc
dir first c/
dir second ca
dir second cb
dir second cc
我发现通过DIR here迭代的方式,但是这是列出了所有第二级儿童也是一样,也是他们的完整路径,而我只想要像dir_first a
等目录名:
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir_path),
RecursiveIteratorIterator::SELF_FIRST);
foreach($iterator as $file) {
if($file->isDir()) {
echo $file->getRealpath();
}
}
如果我理解你的问题的权利,你需要的是输出,但现在没有完整的路径同样的事情 - 用[基名(http://php.net/manual/en/function.basename.php )。 'echo basename($ file-> getRealpath());' – DannyPhantom
是不是会输出最后一个或在这个例子中的第二级目录? – KeepMove
所以你只需要dir_first a,dir_first b等? – DannyPhantom