PHP Doku:: The FilterIterator class - class.filteriterator.html

Verlauf / Chronik / History: (3) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige GrunderweiterungenStandard PHP Library (SPL)IteratorenThe FilterIterator class

Ein Service von Reinhard Neidl - Webprogrammierung.

Iteratoren

<<FilesystemIterator::setFlags

FilterIterator::accept>>


UnterSeiten:

The FilterIterator class

Einführung

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

Klassenbeschreibung

abstract FilterIterator extends IteratorIterator implements OuterIterator , Traversable , Iterator {
/* Methoden */
abstract bool accept ( void )
__construct ( Iterator $iterator )
mixed current ( void )
Iterator getInnerIterator ( void )
mixed key ( void )
void next ( void )
void rewind ( void )
bool valid ( void )
}

Inhaltsverzeichnis


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
Venelin Vulkov
5.11.2008 9: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)