📜  PHP | ArrayIterator current()函数(1)

📅  最后修改于: 2023-12-03 15:33:31.819000             🧑  作者: Mango

PHP | ArrayIterator current()函数

简介

current() 函数用于返回迭代器当前指向的元素。

ArrayIterator 类中,当前指向的元素是迭代器数组中的当前元素。

语法
public mixed ArrayIterator::current ( void )
参数

无参数。

返回值

当前指向的元素。

如果迭代器已经到达数组的末尾,则返回 false

示例
$array = array('apple', 'banana', 'orange');
$iterator = new ArrayIterator($array);

echo $iterator->current(); // 输出 "apple"

$iterator->next();
echo $iterator->current(); // 输出 "banana"

$iterator->next();
echo $iterator->current(); // 输出 "orange"

$iterator->next();
echo $iterator->current(); // 输出 "false"
备注

注意,current() 函数只返回当前指向的元素,并不会将当前指针向后移动。如果需要将指针向后移动,需要使用 next() 函数。

参考资料

PHP ArrayIterator current() 函数