略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: RecursiveFilterIterator::__construct

2024-11-13

RecursiveFilterIterator::__construct

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

RecursiveFilterIterator::__constructCreate a RecursiveFilterIterator from a RecursiveIterator

说明

public RecursiveFilterIterator::__construct(RecursiveIterator $iterator)

Create a RecursiveFilterIterator from a RecursiveIterator.

参数

iterator

The RecursiveIterator to be filtered.

范例

示例 #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";
}
?>

以上例程的输出类似于:

test1
test3
test4
test5

示例 #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";
}
?>

以上例程的输出类似于:

test1
test3
test4
test5

参见

add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/recursivefilteriterator.construct.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3