介绍
创新互联长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为天峨企业提供专业的网站设计、成都做网站,天峨网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。
可在内部迭代自己的外部迭代器或类的接口。
Iterator extends Traversable { /* 方法 */ abstract public mixed current(void) abstract public scalar key(void) abstract public void next(void) abstract public void rewind(void ) abstract public boolean valid(void) }
举个栗子,定义一个迭代器:
class myIerator implements Iterator { protected $position;//指针 public $arr = []; public function __construct(array $arr) { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); $this->arr = $arr; return $this; } //获取当前的值 public function current() { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); return $this->arr[$this->position] ?? null; } //将指针移至下一位 public function next() { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); return ++$this->position; } //返回当前的指针 public function key() { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); return $this->position; } //重置指针 public function rewind() { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); $this->position = 0; } //检查当前是否有效 public function valid() { $info = date('Y-m-d H:i:s') . ' ' . __METHOD__ . ' Line :' . __LINE__ . "\r\n"; $this->writeLog($info); return isset($this->arr[$this->position]); } private function writeLog(string $info) { error_log($info, 3, './debug.log'); } }
$zhangsan=['one','two','three','four']; $arr = new myIerator($zhangsan); foreach ($arr as $k => $v) { echo "{$k} => {$v} \r\n"; }
打印结果:
0 => one
1 => two
2 => three
3 => four
查看调用日志:
2017-11-27 15:56:04 myIerator::__construct Line :16
2017-11-27 15:56:04 myIerator::rewind Line :50 重置指针
2017-11-27 15:56:04 myIerator::valid Line :58 校验是否有效
2017-11-27 15:56:04 myIerator::current Line :25 获取当前指针的值
2017-11-27 15:56:04 myIerator::key Line :41 获取当前指针位置
2017-11-27 15:56:04 myIerator::next Line :33 指针下移一位(进入下一个循环)
2017-11-27 15:56:04 myIerator::valid Line :58
2017-11-27 15:56:04 myIerator::current Line :25
2017-11-27 15:56:04 myIerator::key Line :41
2017-11-27 15:56:04 myIerator::next Line :33
2017-11-27 15:56:04 myIerator::valid Line :58
2017-11-27 15:56:04 myIerator::current Line :25
2017-11-27 15:56:04 myIerator::key Line :41
2017-11-27 15:56:04 myIerator::next Line :33
2017-11-27 15:56:04 myIerator::valid Line :58
2017-11-27 15:56:04 myIerator::current Line :25
2017-11-27 15:56:04 myIerator::key Line :41
2017-11-27 15:56:04 myIerator::next Line :33
2017-11-27 15:56:04 myIerator::valid Line :58
//遍历方式二:
$arr->rewind();
while($arr->valid()) {
$key=$arr->key();
$value=$arr->current();
echo"{$key} =>{$value}\r";
$arr->next();
}
本文名称:PHP迭代器Iterator接口
URL网址:https://www.cdcxhl.com/article46/jdijeg.html
成都网站建设公司_创新互联,为您提供网站收录、虚拟主机、网站制作、外贸建站、微信小程序、网站策划
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联