迭代器
在线手册:中文  英文

The InfiniteIterator class

(PHP 5 >= 5.1.0)

简介

The InfiniteIterator allows one to infinitely iterate over an iterator without having to manually rewind the iterator upon reaching its end.

类摘要

InfiniteIterator extends IteratorIterator implements OuterIterator {
/* 方法 */
public __construct ( Iterator $iterator )
public void next ( void )
/* 继承的方法 */
public mixed IteratorIterator::current ( void )
public Traversable IteratorIterator::getInnerIterator ( void )
public scalar IteratorIterator::key ( void )
public void IteratorIterator::next ( void )
public void IteratorIterator::rewind ( void )
public bool IteratorIterator::valid ( void )
}

Table of Contents


迭代器
在线手册:中文  英文

用户评论:

vascowhite at gmail dot com (2013-06-04 13:54:09)

It is important to realise that rewind() must be called on any iterator before using it or you may experience undefined behaviour, see example code and output here http://3v4l.org/rvNpU
See this bug report https://bugs.php.net/bug.php?id=63823&edit=2 for a fuller explanation.

Anonymous (2012-10-16 00:47:36)

to loop through object keys and reset to the start, try this:
<?php

$obj 
= new stdClass();
$obj->Mon "Monday";
$obj->Tue "Tuesday";
$obj->Wed "Wednesday";
$obj->Thu "Thursday";
$obj->Fri "Friday";
$obj->Sat "Saturday";
$obj->Sun "Sunday";

$infinate = new InfiniteIterator(new ArrayIterator($obj));
foreach ( new 
LimitIterator($infinate014) as $value ) {
    print(
$value PHP_EOL);
}

?>

will output:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Can be useful when doing date operations or recurring events

易百教程