迭代器
在线手册:中文  英文

The RecursiveFilterIterator class

(PHP 5 >= 5.1.0)

简介

This abstract iterator filters out unwanted values for a RecursiveIterator. This class should be extended to implement custom filters. The RecursiveFilterIterator::accept() must be implemented in the subclass.

类摘要

abstract RecursiveFilterIterator extends FilterIterator implements OuterIterator , RecursiveIterator {
/* 方法 */
public __construct ( RecursiveIterator $iterator )
public void getChildren ( void )
public void hasChildren ( void )
/* 继承的方法 */
public abstract bool FilterIterator::accept ( void )
public mixed FilterIterator::current ( void )
public Iterator FilterIterator::getInnerIterator ( void )
public mixed FilterIterator::key ( void )
public void FilterIterator::next ( void )
public void FilterIterator::rewind ( void )
public bool FilterIterator::valid ( void )
}

Table of Contents


迭代器
在线手册:中文  英文

用户评论:

lemats (2011-05-05 10:14:46)

Recursive directory/file listing, filteres ".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($filterItrRecursiveIteratorIterator::SELF_FIRST);
foreach (
$itr as $filePath => $fileInfo) {
    echo 
$fileInfo->getFilename() . PHP_EOL;
}

?>

Russell Dias (2010-09-28 06:38:32)

I had to create a filter to get values greater than a certain number in a multidimensional array.

Hope someone else finds it helpful:

<?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($iter5);

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

易百教程