InternalIterator::next()

将迭代器移动到下一个元素

首字母:I

函数名称:InternalIterator::next()

适用版本:PHP 5 >= 5.1.0, PHP 7

函数描述:InternalIterator::next() 方法将迭代器移动到下一个元素。

用法:

void InternalIterator::next ( void )

参数: 此函数没有参数。

返回值: 此函数没有返回值。

示例:

class MyIterator implements Iterator {
 private $position = 0;
 private $array = array(
 "firstElement",
 "secondElement",
 "thirdElement",
 ); 
 public function __construct() {
 $this->position = 0;
 } 
 public function rewind() {
 $this->position = 0;
 } 
 public function current() {
 return $this->array[$this->position];
 } 
 public function key() {
 return $this->position;
 } 
 public function next() {
 ++$this->position;
 } 
 public function valid() {
 return isset($this->array[$this->position]);
 } 
}
$it = new MyIterator;
$it->rewind();
while ($it->valid()) {
 echo $it->key() . ' => ' . $it->current() . "\n";
 $it->next();
}

输出:

0 => firstElement
1 => secondElement
2 => thirdElement

上述示例中,我们定义了一个名为MyIterator的类,实现了Iterator接口,并实现了next()方法。在next()方法中,我们将$position属性自增1,以便在迭代过程中移动到下一个元素。然后,我们通过创建一个MyIterator对象,并使用rewind()方法将迭代器指针重置到第一个元素。然后,我们使用valid()方法来检查迭代器是否仍然有效,如果有效,则使用key()方法获取当前元素的键值,使用current()方法获取当前元素的值,并通过echo语句输出。最后,我们使用next()方法将迭代器移动到下一个元素,直到迭代器不再有效为止。

同首字母函数