Another fine Iterator from php . You can use it especially when you have to iterate over objects
<?php
$fruits = array(
"apple" => "yummy",
"orange" => "ah ya, nice",
"grape" => "wow, I love it!",
"plum" => "nah, not me"
);
$obj = new ArrayObject( $fruits );
$it = $obj->getIterator();
// How many items are we iterating over?
echo "Iterating over: " . $obj->count() . " values\n";
// Iterate over the values in the ArrayObject:
while( $it->valid() )
{
echo $it->key() . "=" . $it->current() . "\n";
$it->next();
}
// The good thing here is that it can be iterated with foreach loop
foreach ($it as $key=>$val)
echo $key.":".$val."\n";
/* Outputs something like */
Iterating over: 4 values
apple=yummy
orange=ah ya, nice
grape=wow, I love it!
plum=nah, not me
?>
Regards.
A classe ArrayIterator
Introdução
Este iterator permite remover e modificar valores e chaves quando iterando arrays e objetos.
Quando você quiser iterar o mesmo array múltiplas vezes você precisa instanciar ArrayObject e criar instâncias de ArrayIterator que é consultado usando foreach ou chamando o método getIterator() manualmente.
Sinopse da classe
ArrayIterator
/* Métodos */
}Índice
- ArrayIterator::current — Retorna o valor do elemento atual do array
- ArrayIterator::key — Retorna a chave do elemento atual do array
- ArrayIterator::next — Avança para o próximo elemento
- ArrayIterator::rewind — Recoloca o array no início
- ArrayIterator::seek — Aponta para determinada posição
- ArrayIterator::valid — Verifica se o array possui mais elementos
ArrayIterator
Venelin Vulkov
11-Nov-2008 03:44
11-Nov-2008 03:44
