RecursiveFilterIterator: : _ _construct
(PHP 5 >= 5.1.0, PHP 7)
RecursiveFilterIterator::__construct — Create a RecursiveFilterIterator from a RecursiveIterator
Description
Create a RecursiveFilterIterator from a RecursiveIterator.
Examples
Example #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";
}
?>
The above example will output something similar to:
test1 test3 test4 test5
Example #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";
}
?>
The above example will output something similar to:
test1 test3 test4 test5
See Also
- 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
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-recursivefilteriterator.construct.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.