The FilterIterator class
Introduction
...
Class synopsis
FilterIterator
abstract FilterIterator extends IteratorIterator implements OuterIterator , Traversable , Iterator {
/* Methods */
}Table of Contents
FilterIterator::current — Get the current element value FilterIterator::getInnerIterator — Get the inner iterator FilterIterator::key — Get the current key FilterIterator::next — Move the iterator forward FilterIterator::rewind — Rewind the iterator FilterIterator::valid — Check whether the current element is valid
add a note User Contributed NotesFilterIterator
Venelin Vulkov
05-Nov-2008 09:36
05-Nov-2008 09:36
The code below is a simple example of usage . Note that the method which does the actual job is accept.
<?php
class UserFilter extends FilterIterator
{
private $userFilter;
public function __construct(Iterator $iterator , $filter )
{
parent::__construct($iterator);
$this->userFilter = $filter;
}
public function accept()
{
$user = $this->getInnerIterator()->current();
if( strcasecmp($user['name'],$this->userFilter) == 0) {
return false;
}
return true;
}
}
$array = array(
array('name' => 'Jonathan','id' => '5'),
array('name' => 'Abdul' ,'id' => '22')
);
$object = new ArrayObject($array);
// Note it is case insensitive check in our example due the usage of strcasecmp function
$iterator = new UserFilter($object->getIterator(),'abdul');
foreach ($iterator as $result) {
echo $result['name'];
}
/* Outputs Jonathan */
?>
Regards.

DirectoryIterator::valid