我使用的代码显示了我目录中的所有文件夹和子文件夹。
我有一个简单的问题..某些结果是重复的或重复的,我不想显示它。
我该怎么做?
<?php
$dir = 'apps/';
$result = array();
if (is_dir($dir)) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if (!$file->isFile()) {
$result = $file->getPath()."<br>";
echo $result;
}
}
}
?>
答案 0 :(得分:1)
试试这个
<?php
$dir = 'apps/';
$result = array();
if (is_dir($dir)) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if (!$file->isFile()) {
$path = $file->getPath();
if(in_array($path, $result)) {
continue ;
}
$result = $path."<br>";
echo $result;
}
}
}
?>
答案 1 :(得分:0)
您可以使用哈希数组来检查列表中是否已有路径
<?php
$dir = 'apps/';
$result = array();
$hash=array();
if (is_dir($dir)) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if (!$file->isFile()) {
$path = $file->getPath();
if(isset($hash[$path])) {
continue ;
}
$hash[$path]=1;
$result[] = $path;
echo $path."<br>";
}
}
}
?>
答案 2 :(得分:0)
使用array_unique()
<?php
$dir = 'apps/';
$result = array();
if(is_dir($dir)){
$iterator = new RecursiveDirectoryIterator($dir);
foreach(new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file){
if(!$file->isFile()){
$result[] = $file->getPath();
}
}
$uniqueResult = array_unique($result);
if(!empty($uniqueResult)){
foreach($uniqueResult as $v){ // don't use 'for' use 'foreach' here.
echo $v.'<br>';
}
}
}