函式名稱:IntlIterator::rewind()
適用版本:PHP 5 >= 5.5.0, PHP 7, PHP 8
函式描述:IntlIterator::rewind() 方法重置迭代器到起始位置。
用法:
bool IntlIterator::rewind ( void )
引數:
- 無引數
返回值:
- 當迭代器成功重置到起始位置時返回 true,否則返回 false。
示例:
$collator = new Collator('en_US');
// 獲取排序後的陣列
$sortedArray = $collator->sort(['apple', 'banana', 'cherry']);
// 建立 IntlIterator 物件
$iterator = new IntlIterator($sortedArray);
// 遍歷迭代器並輸出每個元素
foreach ($iterator as $key => $value) {
echo "Key: $key, Value: $value" . PHP_EOL;
}
// 重置迭代器到起始位置
$iterator->rewind();
// 再次遍歷迭代器並輸出每個元素
foreach ($iterator as $key => $value) {
echo "Key: $key, Value: $value" . PHP_EOL;
}
輸出:
Key: 0, Value: apple
Key: 1, Value: banana
Key: 2, Value: cherry
Key: 0, Value: apple
Key: 1, Value: banana
Key: 2, Value: cherry
以上示例展示瞭如何使用 IntlIterator::rewind() 方法重置迭代器到起始位置。首先,我們建立了一個排序後的陣列,並將其傳遞給 IntlIterator 物件進行迭代。然後,我們使用 foreach 迴圈遍歷迭代器並輸出每個元素的鍵和值。接下來,我們呼叫 rewind() 方法將迭代器重置到起始位置,並再次使用 foreach 迴圈遍歷迭代器,輸出結果與之前相同。