RecursiveFilterIterator 类

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

简介

此抽象迭代器会过滤掉 RecursiveIterator 中不需要的值。此类应扩展以实现自定义过滤器。子类必须实现 RecursiveFilterIterator::accept() 方法。

类概要

目录

添加注释

用户贡献的注释 2 条注释

lemats
13 年前
递归目录/文件列表,过滤掉 ".svn"

<?php

class MyRecursiveFilterIterator extends RecursiveFilterIterator {

public static
$FILTERS = array(
'.svn',
);

public function
accept() {
return !
in_array(
$this->current()->getFilename(),
self::$FILTERS,
true
);
}

}

$dirItr = new RecursiveDirectoryIterator('/sample/path');
$filterItr = new MyRecursiveFilterIterator($dirItr);
$itr = new RecursiveIteratorIterator($filterItr, RecursiveIteratorIterator::SELF_FIRST);
foreach (
$itr as $filePath => $fileInfo) {
echo
$fileInfo->getFilename() . PHP_EOL;
}

?>
Russell Dias
13 年前
我不得不创建一个过滤器来获取多维数组中大于某个特定数字的值。

希望其他人觉得它有用

<?php
class GreaterThanFilter extends RecursiveFilterIterator {
protected
$limit;

public function
__construct(RecursiveIterator $recursiveIter, $limit) {
$this->limit = $limit;
parent::__construct($recursiveIter);
}
public function
accept() {
return
$this->hasChildren() || strlen($this->current()) >= $this->limit;
}
public function
getChildren() {
return new
self($this->getInnerIterator()->getChildren(), $this->limit);
}
}
$iter = new RecursiveArrayIterator($array);
$filter = new GreaterThanFilter($iter, 5);

foreach(new
RecursiveIteratorIterator($filter) as $key => $value)
{
echo
$value . "\n";
}
?>
To Top