PHP 8.5.0 Beta 3 available for testing

ArrayIterator クナ゚

(PHP 5, PHP 7, PHP 8)

はじめãĢ

こぎイテãƒŦãƒŧã‚ŋは、配列やã‚Ēブジェクトを反垊å‡Ļį†ã™ã‚‹éš›ãĢ 値やキãƒŧをäŋŽæ­Ŗã—たり、čĻį´ ã‚’å‰Šé™¤ã™ã‚‹ã“ã¨ãŒã§ããžã™ã€‚

同じ配列をäŊ•åēĻも反垊å‡Ļį†ã—ãŸã„å ´åˆã¯ã€ ArrayObject ãŽã‚¤ãƒŗã‚šã‚ŋãƒŗã‚šã¨ãã‚Œã‚’å‚į…§ã™ã‚‹ ArrayIterator ãŽã‚¤ãƒŗã‚šã‚ŋãƒŗã‚šã‚’äŊœæˆã—、 foreach をäŊŋį”¨ã™ã‚‹ã‹ ArrayObject::getIterator() ãƒĄã‚Ŋãƒƒãƒ‰ã‚’æ‰‹å‹•ã§ã‚ŗãƒŧãƒĢしぞす。

クナ゚æĻ‚čρ

class ArrayIterator implements SeekableIterator, ArrayAccess, Serializable, Countable {
/* 厚数 */
public const int STD_PROP_LIST;
public const int ARRAY_AS_PROPS;
/* ãƒĄã‚Ŋッド */
public __construct(array|object $array = [], int $flags = 0)
public append(mixed $value): void
public asort(int $flags = SORT_REGULAR): true
public count(): int
public current(): mixed
public getFlags(): int
public key(): string|int|null
public ksort(int $flags = SORT_REGULAR): true
public natcasesort(): true
public natsort(): true
public next(): void
public offsetExists(mixed $key): bool
public offsetGet(mixed $key): mixed
public offsetSet(mixed $key, mixed $value): void
public offsetUnset(mixed $key): void
public rewind(): void
public seek(int $offset): void
public serialize(): string
public setFlags(int $flags): void
public uasort(callable $callback): true
public uksort(callable $callback): true
public unserialize(string $data): void
public valid(): bool
}

åŽšįžŠæ¸ˆãŋ厚数

ArrayIterator ぎフナグ

ArrayIterator::STD_PROP_LIST

ã‚Ēãƒ–ã‚¸ã‚§ã‚¯ãƒˆãŽãƒ—ãƒ­ãƒ‘ãƒ†ã‚ŖãŒã€ ãƒĒ゚トとしãĻ(var_dump(), foreach ãĒおから)ã‚ĸクã‚ģ゚されたときぎ通常ぎ抟čƒŊを持つ

ArrayIterator::ARRAY_AS_PROPS

ã‚¨ãƒŗãƒˆãƒĒãŒãƒ—ãƒ­ãƒ‘ãƒ†ã‚Ŗã¨ã—ãĻã‚ĸクã‚ģ゚できる(čĒ­ãŋ書きともãĢ)

į›ŽæŦĄ

īŧ‹add a note

User Contributed Notes 4 notes

up
53
Venelin Vulkov Âļ
16 years ago
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.
up
19
Relakuyae Âļ
13 years ago
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"));
?>
up
2
butesa at freenet dot de Âļ
2 years ago
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
= [1 => 'a', 2 => '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
= [1 => 'a', 2 => 'b'];
$array2 = [1 => 'a', 2 => 'b'];
$array3 = [1 => 'a', 2 => '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);
?>
up
11
Sean Burlington Âļ
16 years ago
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
To Top