🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
遍历并过滤出不想要的值 ``` abstract FilterIterator extends IteratorIterator implements OuterIterator { /* 新增的抽象方法 */ public abstract accept ( void ) : bool -- 检查迭代器的当前元素是否可接受 /* 重写方法 */ public getInnerIterator ( void ) : Iterator -- 获取内部迭代器 public __construct ( Iterator $iterator ) /* 继承方法 */ public current ( void ) : mixed public key ( void ) : mixed public next ( void ) : void public rewind ( void ) : void public valid ( void ) : bool } ``` 例子: ``` class RecursiveFileFilterIterator extends FilterIterator { // 满足条件的扩展名 protected $ext = array('jpg','gif'); /** * 提供 $path 并生成对应的目录迭代器 */ public function __construct($path) { parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path))); } /** * 检查文件扩展名是否满足条件 */ public function accept() { $item = $this->getInnerIterator(); if ($item->isFile() && in_array(pathinfo($item->getFilename(), PATHINFO_EXTENSION), $this->ext)) { return TRUE; } } } ```