(PHP 5 >= 5.1.0, PHP 7)
Create a RecursiveFilterIterator from a RecursiveIterator
public RecursiveFilterIterator::__construct ( RecursiveIterator $iterator )
Create a RecursiveFilterIterator from a RecursiveIterator.
Parameters:
iterator
The RecursiveIterator to be filtered.
Returns:
No value is returned.
Examples:
Basic RecursiveFilterIterator() example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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
RecursiveFilterIterator() example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?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() -
Please login to continue.