(PHP 5 >= 5.1.0, PHP 7, PHP 8)
FilterIterator::accept — 检查迭代器的当前元素是否可接受
此函数没有参数。
示例 #1 FilterIterator::accept() 示例
<?php
// 此迭代器过滤所有字符数小于 10 的值
class LengthFilterIterator extends FilterIterator {
public function accept() {
// 仅接受长度为 10 或大于 10 的字符串
return strlen(parent::current()) >= 10;
}
}
$arrayIterator = new ArrayIterator(array('test1', 'more than 10 characters'));
$lengthFilter = new LengthFilterIterator($arrayIterator);
foreach ($lengthFilter as $value) {
echo $value . "\n";
}
?>
上面的例子将输出
more than 10 characters