(PHP 5 >= 5.1.0, PHP 7, PHP 8)
FilterIterator::accept — 检查迭代器的当前元素是否可接受
此函数没有参数。
示例 #1 FilterIterator::accept() 示例
<?php
// 此迭代器过滤所有字符数少于 10 的值
class LengthFilterIterator extends FilterIterator {
public function accept() {
// 仅接受长度为 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