PHP SPL 迭代器
2010-05-25 20:34 ScriptZhang 阅读(701) 评论(0) 编辑 收藏 举报SPL提供了5个迭代器接口:Traversable.Iterator.IteratorAggregate.OuterIterator ,RecurseiveIterator。
使用IteratorAggregate接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class MyIterableClass implements IteratorAggregate{ protected $arr ; public function __construct(){ $this ->arr= array (1,2,3); } public function getIterator(){ return new ArrayIterator( $this ->arr); } } foreach ( new MyIterableClass() as $value ){ echo $value . "\n" ; } 输出如下 |
1 | <img src= "https://pic002.cnblogs.com/img/hacks/201005/2010052520085872.jpg" > |
1 | 使用ArrayIterator迭代器 |
1 2 3 4 5 6 7 | <pre class = "brush:php" > $arr = array ( 'a' , 'b' , 'c' ); $iterator = new ArrayIterator( $arr ); foreach ( $iterator as $val ){ echo $val ; } </pre> <img src= "https://pic002.cnblogs.com/img/hacks/201005/2010052520124176.jpg" > |
1 | 使用LimitIterator 迭代器 |
1 2 3 4 5 6 7 8 | <pre class = "brush:php" > $arr = array (1,2,3,4,5,6,7,8); $arrIterator = new ArrayIterator( $arr ); $limitIterator = new LimitIterator( $arrIterator ,3,4); foreach ( $limitIterator as $number ){ echo $number ; } </pre> <img src= "https://pic002.cnblogs.com/img/hacks/201005/2010052520155564.jpg" > |
1 | 使用AppendIterator迭代器 |
1 2 3 4 5 6 7 8 9 10 11 12 13 | <pre class = "brush:php" > $arrFirst = new ArrayIterator( array (1,2,3)); $arrSecond = new ArrayIterator( array (4,5,6)); $iterator = new AppendIterator(); $iterator ->append( $arrFirst ); $iterator ->append( $arrSecond ); foreach ( $iterator as $number ){ echo $number ; } </pre> <img src= "https://pic002.cnblogs.com/img/hacks/201005/2010052520194091.jpg" > |
1 | 高级的数组合并功能 |
1 2 3 4 5 6 7 8 | $arrFirst = new ArrayIterator( array (1,2,3)); $arrSecond = new ArrayIterator( array (4,5,6)); $iterator = new AppendIterator(); $iterator ->append( new LimitIterator( $arrFirst ,0,2)); $iterator ->append( new LimitIterator( $arrSecond ,0,2)); print_r(iterator_to_array( $iterator ,false)); |
1 | 使用FilterIterator 迭代器 |
1 2 3 4 5 6 7 8 9 | <pre class = "brush:php" > $arrFirst = new ArrayIterator( array (1,2,3)); $arrSecond = new ArrayIterator( array (4,5,6)); $iterator = new AppendIterator(); $iterator ->append( new LimitIterator( $arrFirst ,0,2)); $iterator ->append( new LimitIterator( $arrSecond ,0,2)); print_r(iterator_to_array( $iterator ,false)); </pre> |
1 | <img src= "https://pic002.cnblogs.com/img/hacks/201005/2010052520282175.jpg" > |
1 | 使用一个基本的RegexIterator迭代器 |
1 2 3 4 5 6 7 | <pre class = "brush:php" > $arr = array ( 'apple' , 'avocado' , 'orange' , 'pineapple' ); $arrIterator = new ArrayIterator( $arr ); $iterator = new RegexIterator( $arrIterator , '/^a/' ); print_r(iterator_to_array( $iterator )); </pre> <img src= "https://pic002.cnblogs.com/img/hacks/201005/2010052520313425.jpg" > |
1 |
1 |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· 开发者新选择:用DeepSeek实现Cursor级智能编程的免费方案
· 【译】.NET 升级助手现在支持升级到集中式包管理
· 独立开发经验谈:如何通过 Docker 让潜在客户快速体验你的系统
· 并发编程 - 线程同步(二)