Iteradores
PHP Manual

La clase LimitIterator

Introducción

La clase LimitIterator permite una iteración de un limitado subgrupo de ítems en un Iterator.

Clases sinopsis

LimitIterator extends IteratorIterator implements OuterIterator , Traversable , Iterator {
/* Métodos */
__construct ( Iterator $iterator [, int $offset = 0 [, int $count = -1 ]] )
public mixed current ( void )
public Iterator getInnerIterator ( void )
public int getPosition ( void )
public mixed key ( void )
public void next ( void )
public void rewind ( void )
public int seek ( int $position )
public bool valid ( void )
}

Ejemplos

Example #1 Ejemplo de uso de LimitIterator

<?php

// Crea un iterador a ser limitado
$fruits = new ArrayIterator(array(
    
'apple',
    
'banana',
    
'cherry',
    
'damson',
    
'elderberry'
));

// Recorre el bucle sólo para las tres primeras frutas
foreach (new LimitIterator($fruits03) as $fruit) {
    
var_dump($fruit);
}

echo 
"\n";

// Recorre el bucle desde las tercera fruta asta el final
// Nota: empieza por 0, por manzana
foreach (new LimitIterator($fruits2) as $fruit) {
    
var_dump($fruit);
}

?>

El resultado del ejemplo sería:

string(5) "apple"
string(6) "banana"
string(6) "cherry"

string(6) "cherry"
string(6) "damson"
string(10) "elderberry"

Table of Contents


Iteradores
PHP Manual