ArrayIterator 类
(PHP 5, PHP 7, PHP 8)
简介
允许在遍历 arrays 或 objects 时删除元素,修改键或值。
要多次迭代同一个数组,建议实例化 ArrayObject 并使用
ArrayIterator 实例,当使用 foreach
隐式创建时,可以迭代内部存储的数组,
或者通过手动调用 ArrayObject::getIterator() 方法创建一个。
类摘要
/* 常量 */
/* 方法 */
}预定义常量
ArrayIterator 标记
ArrayIterator::STD_PROP_LIST
-
当以列表形式访问对象时(例如 var_dump()、
foreach
等),对象的属性将具有正常的功能。 ArrayIterator::ARRAY_AS_PROPS
-
可以通过属性访问条目(读写都支持)。
目录
- ArrayIterator::append — Append an element
- ArrayIterator::asort — Sort entries by values
- ArrayIterator::__construct — Construct an ArrayIterator
- ArrayIterator::count — Count elements
- ArrayIterator::current — Return current array entry
- ArrayIterator::getArrayCopy — Get array copy
- ArrayIterator::getFlags — Get behavior flags
- ArrayIterator::key — Return current array key
- ArrayIterator::ksort — Sort entries by keys
- ArrayIterator::natcasesort — Sort entries naturally, case insensitive
- ArrayIterator::natsort — Sort entries naturally
- ArrayIterator::next — Move to next entry
- ArrayIterator::offsetExists — Check if offset exists
- ArrayIterator::offsetGet — Get value for an offset
- ArrayIterator::offsetSet — Set value for an offset
- ArrayIterator::offsetUnset — Unset value for an offset
- ArrayIterator::rewind — Rewind array back to the start
- ArrayIterator::seek — Seeks to a position
- ArrayIterator::serialize — Serialize
- ArrayIterator::setFlags — Set behaviour flags
- ArrayIterator::uasort — Sort with a user-defined comparison function and maintain index association
- ArrayIterator::uksort — Sort by keys using a user-defined comparison function
- ArrayIterator::unserialize — Unserialize
- ArrayIterator::valid — Check whether array contains more entries
+添加备注
用户贡献的备注 4 notes
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.
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"));
?>
Sean Burlington ¶
15 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
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);
?>