函式名:LimitIterator::current()
適用版本:PHP 5 >= 5.1.0, PHP 7
用法:LimitIterator::current() 函式用於返回當前迭代器指向的元素。
語法:public mixed LimitIterator::current ( void )
引數:無引數
返回值:返回當前迭代器指向的元素。如果當前位置無效,則返回NULL。
示例:
$array = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
$iterator = new ArrayIterator($array);
$limitIterator = new LimitIterator($iterator, 1, 3);
echo $limitIterator->current(); // 輸出:banana
在上面的示例中,我們首先建立了一個包含5個元素的陣列。然後,我們使用ArrayIterator
類將陣列轉換為迭代器。接下來,我們使用LimitIterator
類建立一個限制範圍的迭代器,該迭代器從索引位置1開始,限制在3個元素內。最後,我們使用current()
函式獲取當前迭代器指向的元素,即第二個元素"banana"。