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"));
?>
ArrayIterator クラス
(PHP 5)
導入
このイテレータは、配列やオブジェクトを反復処理する際に 値やキーをリセットしたり修正したりすることができます。
同じ配列を何度も反復処理したい場合は、 ArrayObject のインスタンスとそれを参照する ArrayIterator のインスタンスを作成し、 foreach を使用するか getIterator() メソッドを手動でコールします。
クラス概要
ArrayIterator
implements
Iterator
,
Traversable
,
ArrayAccess
,
SeekableIterator
,
Countable
,
Serializable
{
/* メソッド */
}目次
- ArrayIterator::append — 要素を追加する
- ArrayIterator::asort — 値で配列をソートする
- ArrayIterator::__construct — ArrayIterator を作成する
- ArrayIterator::count — 要素を数える
- ArrayIterator::current — 現在の配列エントリを返す
- ArrayIterator::getArrayCopy — 配列のコピーを取得する
- ArrayIterator::getFlags — フラグを取得する
- ArrayIterator::key — 現在の配列キーを返す
- ArrayIterator::ksort — キーで配列をソートする
- ArrayIterator::natcasesort — 大文字小文字を区別せずに自然順で配列をソートする
- ArrayIterator::natsort — 自然順で配列をソートする
- ArrayIterator::next — 次のエントリに移動する
- ArrayIterator::offsetExists — オフセットが存在するかどうかを調べる
- ArrayIterator::offsetGet — オフセットの値を取得する
- ArrayIterator::offsetSet — オフセットの値を設定する
- ArrayIterator::offsetUnset — オフセットの値を削除する
- ArrayIterator::rewind — 配列を最初に巻き戻す
- ArrayIterator::seek — 位置を移動する
- ArrayIterator::serialize — シリアライズする
- ArrayIterator::setFlags — 振る舞いのフラグを設定する
- ArrayIterator::uasort — ユーザー定義のソート
- ArrayIterator::uksort — ユーザー定義のソート
- ArrayIterator::unserialize — アンシリアライズする
- ArrayIterator::valid — 配列がまだエントリを持っているかどうかチェックする
Relakuyae
19-Oct-2011 11:23
foobuilder at gmail dot com
09-Dec-2010 04: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
02-Dec-2009 11: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
25-May-2009 11: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
10-Nov-2008 08: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.
