downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ArrayIterator::current> <AppendIterator::valid
Last updated: Fri, 14 Aug 2009

view this page in

La classe ArrayIterator

Introduction

Cet itérateur permet de réinitialiser et de modifier les valeurs et les clés lors de l'itération de tableaux et d'objets.

Lorsque vous voulez passer en revue le même tableau plusieurs fois, vous devez instancier ArrayObject et le laisser créer les objets ArrayIterator qui s'y réfère, soit en utilisant l'instruction foreach, soit en appelant la méthode getIterator()() manuellement.

Synopsis de la classe

ArrayIterator
ArrayIterator implements Iterator , Traversable , ArrayAccess , SeekableIterator , Countable {
/* Méthodes */
mixed current ( void )
mixed key ( void )
void next ( void )
void rewind ( void )
void seek ( int $position )
bool valid ( void )
}

Sommaire



add a note add a note User Contributed Notes
ArrayIterator
Sean Burlington
26-May-2009 12:15
and to iterate recursively use the (sparsely documented)  RecursiveArrayIterator

<?php

$fruits
= array(
               
"apple" => "yummy",
               
"orange" => "ah ya, nice",
               
"grape" => "wow, I love it!",
                
"plum" => "nah, not me"
               
);

$veg = array("potato" => "chips", "carrot" => "soup");
$grocery = array($fruits, $veg);
$obj = new ArrayObject( $grocery );

$it = new RecursiveIteratorIterator( new RecursiveArrayIterator($grocery));

foreach (
$it as $key=>$val)
echo
$key.":".$val."\n";

?>

Output
--------
apple:yummy
orange:ah ya, nice
grape:wow, I love it!
plum:nah, not me
potato:chips
carrot:soup
Venelin Vulkov
11-Nov-2008 11:44
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.

ArrayIterator::current> <AppendIterator::valid
Last updated: Fri, 14 Aug 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites