RecursiveFilterIterator::__construct
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
RecursiveFilterIterator::__construct — Create a RecursiveFilterIterator from a RecursiveIterator
Beschreibung
Create a RecursiveFilterIterator from a RecursiveIterator.
Beispiele
Beispiel #1 Basic RecursiveFilterIterator() example
<?php
class TestsOnlyFilter extends RecursiveFilterIterator {
public function accept() {
// Accept the current item if we can recurse into it
// or it is a value starting with "test"
return $this->hasChildren() || (strpos($this->current(), "test") !== FALSE);
}
}
$array = array("test1", array("taste2", "test3", "test4"), "test5");
$iterator = new RecursiveArrayIterator($array);
$filter = new TestsOnlyFilter($iterator);
foreach(new RecursiveIteratorIterator($filter) as $key => $value)
{
echo $value . "\n";
}
?>
Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:
test1 test3 test4 test5
Beispiel #2 RecursiveFilterIterator() example
<?php
class StartsWithFilter extends RecursiveFilterIterator {
protected $word;
public function __construct(RecursiveIterator $rit, $word) {
$this->word = $word;
parent::__construct($rit);
}
public function accept() {
return $this->hasChildren() OR strpos($this->current(), $this->word) === 0;
}
public function getChildren() {
return new self($this->getInnerIterator()->getChildren(), $this->word);
}
}
$array = array("test1", array("taste2", "test3", "test4"), "test5");
$iterator = new RecursiveArrayIterator($array);
$filter = new StartsWithFilter($iterator, "test");
foreach(new RecursiveIteratorIterator($filter) as $key => $value)
{
echo $value . "\n";
}
?>
Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:
test1 test3 test4 test5
Siehe auch
- RecursiveFilterIterator::getChildren() - Return the inner iterator's children contained in a RecursiveFilterIterator
- RecursiveFilterIterator::hasChildren() - Check whether the inner iterator's current element has children
- FilterIterator::accept() - Check whether the current element of the iterator is acceptable