The ArrayIterator class

(PHP 5)

Introduction

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.

Class synopsis

ArrayIterator implements ArrayAccess , SeekableIterator , Countable , Serializable {
/* Methods */
public void append ( mixed $value )
public void asort ( void )
public __construct ([ mixed $array = array() [, int $flags = 0 ]] )
public int count ( void )
public mixed current ( void )
public array getArrayCopy ( void )
public void getFlags ( void )
public mixed key ( void )
public void ksort ( void )
public void natcasesort ( void )
public void natsort ( void )
public 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 )
public void rewind ( void )
public 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 )
public bool valid ( void )
}

Table of Contents

Коментарии

Автор:
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 over4 values
apple
=yummy
orange
=ah yanice
grape
=wowI love it!
plum=nahnot me

?>

Regards.
2008-11-11 05:44:02
http://php5.kiev.ua/manual/ru/class.arrayiterator.html
Автор:
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
2009-05-26 08:15:47
http://php5.kiev.ua/manual/ru/class.arrayiterator.html
Автор:
Need a callback on an iterated value, but don't have PHP 5.4+?  This makes is stupid easy:

<?php
class ArrayCallbackIterator extends ArrayIterator {
  private 
$callback;
  public function 
__construct($value$callback) {
   
parent::__construct($value);
   
$this->callback $callback;
  }

  public function 
current() {
   
$value parent::current();
    return 
call_user_func($this->callback$value);
  }
}
?>

You can use it pretty much exactly as the Array Iterator:

<?php
$iterator1 
= new ArrayCallbackIterator($valueList"callback_function");
$iterator2 = new ArrayCallbackIterator($valueList, array($object"callback_class_method"));
?>
2011-10-19 20:23:33
http://php5.kiev.ua/manual/ru/class.arrayiterator.html
The documentation states "This iterator allows to unset and modify values and keys while iterating over Arrays and Objects". But if you pass an array to the constructor, the iterator works with a copy of that array, so the modifications will not be written back to that initial array. ArrayObject behaves the same way.

If you want an iterator that writes back to the array, you can use this function:

<?php
function &getArrayIterator(array &$a): Iterator {
    foreach (
$a as $k => &$v) {
        yield 
$k => $v;
    }
}
?>

Usage:

<?php
$array 
= [=> 'a'=> 'b'];

$iterator getArrayIterator($array);
foreach (
$iterator as &$value) {
   
$value .= 'x';
}

//array(2) {
//  [1]=>
//  string(2) "ax"
//  [2]=>
//  &string(2) "bx"
//}
//object(Generator)#4 (0) {
//}
var_dump($array);
var_dump($iterator);

?>

Comparison with plain array, ArrayIterator and ArrayObject:

<?php
$array1 
= [=> 'a'=> 'b'];
$array2 = [=> 'a'=> 'b'];
$array3 = [=> 'a'=> 'b'];

foreach (
$array1 as &$value) {
   
$value .= 'x';
}

$iterator2 = new ArrayIterator($array2);
foreach (
$iterator2 as &$value) {
   
$value .= 'x';
}

$iterator3 = new ArrayObject($array3);
foreach (
$iterator3 as &$value) {
   
$value .= 'x';
}

//array(2) {
//  [1]=>
//  string(2) "ax"
//  [2]=>
//  string(2) "bx"
//}
var_dump($array1);

//array(2) {
//  [1]=>
//  string(1) "a"
//  [2]=>
//  string(1) "b"
//}
//object(ArrayIterator)#1 (1) {
//  ["storage":"ArrayIterator":private]=>
//  array(2) {
//    [1]=>
//    string(2) "ax"
//    [2]=>
//    string(2) "bx"
//  }
//}
var_dump($array2);
var_dump($iterator2);

//array(2) {
//  [1]=>
//  string(1) "a"
//  [2]=>
//  string(1) "b"
//}
//object(ArrayObject)#2 (1) {
//  ["storage":"ArrayObject":private]=>
//  array(2) {
//    [1]=>
//    string(2) "ax"
//    [2]=>
//    string(2) "bx"
//  }
//}
var_dump($array3);
var_dump($iterator3);
?>
2022-10-28 20:11:57
http://php5.kiev.ua/manual/ru/class.arrayiterator.html

    Поддержать сайт на родительском проекте КГБ