ArrayIterator::current()
是一個 ArrayIterator 類的方法,用於獲取當前指標所指向的元素的值。
用法:
mixed ArrayIterator::current( void )
示例:
<?php
// 建立一個陣列迭代器
$iterator = new ArrayIterator(['apple', 'banana', 'cherry']);
// 獲取當前指標所指向的元素的值
$currentElement = $iterator->current();
// 輸出當前元素的值
echo "當前元素的值: " . $currentElement . "<br>";
?>
返回值:
mixed
:返回當前指標所指向的元素的值。
注意:如果迭代器還沒有被遍歷或已經到達了末尾,current()
方法將返回 NULL
。
使用 ArrayIterator
可以方便地對陣列進行遍歷,並使用 current()
方法獲取當前指標的值。