牛牛在线工具

InternalIterator::current()

收录 8868 个 PHP 函数,含用途说明与用法示例。

InternalIterator::current()

返回内部迭代器中当前指向的元素

函数名称:InternalIterator::current() 适用版本:PHP 5 >= 5.5.0, PHP 7 函数描述:此方法用于返回内部迭代器中当前指向的元素。 用法: mixed InternalIterator::current ( void ) 参数:此函数不接受任何参数。 返回值:返回当前指向的元素。如果没有更多元素可供遍历,则返回 FALSE。 示例: class MyIterator implements Iterator { private $position = 0; private $array = array( "firstElement", "secondElement", "thirdElement" ); 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; foreach($it as $key => $value) { echo $key . ": " . $value . "\n"; } 输出: 0: firstElement 1: secondElement 2: thirdElement 在上面的示例中,我们创建了一个自定义的迭代器类 MyIterator 并实现了 Iterator 接口的方法。其中,current() 方法返回当前指向的元素。我们使用 foreach 循环遍历迭代器对象,并输出每个元素的键和值。