PHP Conference Japan 2024

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
14 年前
我必须创建一个过滤器来获取多维数组中大于某个数字的值。

希望其他人能发现它有用

<?php
GreaterThanFilter 扩展 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