Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / PHP

CachingIterator

Añade anticipación de un elemento. hasNext indica si hay otro tras el actual, útil para insertar separadores.

Versión: PHP 8.4; repository runtime, production runtime not attested

Sintaxis

new CachingIterator($iterator, CachingIterator::FULL_CACHE)

Añade anticipación de un elemento. hasNext indica si hay otro tras el actual, útil para insertar separadores. FULL_CACHE guarda los pares visitados para getCache, consume memoria y sobrescribe entradas con la misma clave.

$it=new CachingIterator(new ArrayIterator(["a"=>2,"b"=>3]),CachingIterator::FULL_CACHE);
$look=[]; foreach($it as $k=>$v){$look[]=[$k,$it->hasNext()];}
echo json_encode([$look,$it->getCache()]);
[[["a",true],["b",false]],{"a":2,"b":3}]

Ejemplos

<?php
$it=new CachingIterator(new ArrayIterator(["a"=>2,"b"=>3]),CachingIterator::FULL_CACHE);
$look=[]; foreach($it as $k=>$v){$look[]=[$k,$it->hasNext()];}
echo json_encode([$look,$it->getCache()]);

Resultado

[[["a",true],["b",false]],{"a":2,"b":3}]

Fuentes