PHP Doku:: The ArrayIterator class - class.arrayiterator.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige GrunderweiterungenStandard PHP Library (SPL)IteratorenThe ArrayIterator class

Ein Service von Reinhard Neidl - Webprogrammierung.

Iteratoren

<<AppendIterator::valid

ArrayIterator::append>>


UnterSeiten:

The ArrayIterator class

Einführung

This iterator allows to unset and modify values and keys while iterating over Arrays and Objects.

When you want to iterate over the same array multiple times you need to instantiate ArrayObject and let it create ArrayIterator instances that refer to it either by using foreach or by calling its getIterator() method manually.

Klassenbeschreibung

ArrayIterator implements Iterator , Traversable , ArrayAccess , SeekableIterator , Countable {
/* Methoden */
public void append ( mixed $value )
public void asort ( void )
__construct ( mixed $array )
public int count ( void )
mixed current ( void )
public array getArrayCopy ( void )
public void getFlags ( void )
mixed key ( void )
public void ksort ( void )
public void natcasesort ( void )
public void natsort ( void )
void next ( void )
public void offsetExists ( string $index )
public mixed offsetGet ( string $index )
public void offsetSet ( string $index , string $newval )
public void offsetUnset ( string $index )
void rewind ( void )
void seek ( int $position )
public string serialize ( void )
public void setFlags ( string $flags )
public void uasort ( string $cmp_function )
public void uksort ( string $cmp_function )
public string unserialize ( string $serialized )
bool valid ( void )
}

Inhaltsverzeichnis


4 BenutzerBeiträge:
- Beiträge aktualisieren...
foobuilder at gmail dot com
9.12.2010 20:01
Unsetting all keys of an ArrayItem within foreach will always leave the second key:

<?php
$items
= new ArrayObject(range(0, 9));

while (list(
$k, $v) = each($items)) {
    unset(
$items[$k]);
}

print_r($items);

//  ArrayIterator Object
//  (
//      [storage:ArrayIterator:private] => Array
//          (
//              [1] => 1
//          )
//  )
?>

I'm not sure if this is a bug as unsetting keys within foreach is usually a bad idea to begin with (use while instead), but it's something to be aware of.
liranuna at liranuna dot com
3.12.2009 3:44
If you want to make your ArrayIterator support foreach loops with PHP's & operator, such as

<?php
   
foreach($list as &$item) {
        ....
    }
?>

You will need to pass the array to ArrayIterator by reference:

<?php
   
new ArrayIterator(&$array);
?>
Sean Burlington
26.05.2009 14: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.11.2008 12: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.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)