PHP函式:AppendIterator::current()
用法:AppendIterator::current() 函式用於獲取當前指標所指向的迭代器的當前元素。
示例:
// 建立兩個迭代器
$array1 = new ArrayIterator(['apple', 'banana', 'cherry']);
$array2 = new ArrayIterator(['dog', 'cat', 'elephant']);
// 建立一個 AppendIterator 物件,並將兩個迭代器新增到其中
$appendIterator = new AppendIterator();
$appendIterator->append($array1);
$appendIterator->append($array2);
// 遍歷 AppendIterator 中的元素,並使用 current() 函式獲取當前元素
while ($appendIterator->valid()) {
echo $appendIterator->current() . "<br>";
$appendIterator->next();
}
輸出結果:
apple<br>
banana<br>
cherry<br>
dog<br>
cat<br>
elephant<br>
在上面的示例中,我們首先建立了兩個 ArrayIterator 物件 $array1
和 $array2
,分別包含了水果和動物。然後,我們建立了一個 AppendIterator 物件 $appendIterator
,並透過 append()
方法將兩個迭代器新增到其中。
接下來,我們使用 while
迴圈遍歷 $appendIterator
,在每次迴圈中使用 current()
函式獲取當前元素,並透過 echo
輸出。透過 next()
方法將指標移向下一個元素。
最終,我們將會得到兩個迭代器中所有元素的輸出結果。