If the inner iterator implements SeekableIterator, LimitIterator uses seek() after rewind() to move to the offset.
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
La clase LimitIterator permite iterar sobre una parte limitada de entidades desde un Iterator.
Ejemplo #1 Ejemplo de uso de LimitIterator
<?php
// Crear un iterador a limitar
$fruits = new ArrayIterator(array(
'apple',
'banana',
'cherry',
'damson',
'elderberry'
));
// Bucle sobre los 3 primeros frutos únicamente
foreach (new LimitIterator($fruits, 0, 3) as $fruit) {
var_dump($fruit);
}
echo "\n";
// Bucle desde el 3º fruto hasta el último
// Nota: la clave comienza en cero para apple
foreach (new LimitIterator($fruits, 2) as $fruit) {
var_dump($fruit);
}
?>
El ejemplo anterior mostrará :
string(5) "apple" string(6) "banana" string(6) "cherry" string(6) "cherry" string(6) "damson" string(10) "elderberry"
If the inner iterator implements SeekableIterator, LimitIterator uses seek() after rewind() to move to the offset.