RecursiveArrayIterator::getChildren

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

RecursiveArrayIterator::getChildren如果当前条目是 数组对象,则返回该条目的迭代器

描述

public RecursiveArrayIterator::getChildren(): ?RecursiveArrayIterator

返回当前迭代器条目的迭代器。

参数

此函数没有参数。

返回值

当前条目的迭代器,如果它是一个 数组对象;或者在失败时为 null

错误/异常

如果当前条目不包含 数组对象,将抛出 InvalidArgumentException

示例

示例 #1 RecursiveArrayIterator::getChildren() 示例

<?php
$fruits
= array("a" => "lemon", "b" => "orange", array("a" => "apple", "p" => "pear"));

$iterator = new RecursiveArrayIterator($fruits);

while (
$iterator->valid()) {

if (
$iterator->hasChildren()) {
// 打印所有子项
foreach ($iterator->getChildren() as $key => $value) {
echo
$key . ' : ' . $value . "\n";
}
} else {
echo
"没有子项。\n";
}

$iterator->next();
}
?>

上面的示例将输出

No children.
No children.
a : apple
p : pear

参见

添加笔记

用户贡献笔记 1 个笔记

814ckf0x
10 年前
RecursiveArrayIterator::getChildrens 返回子项的副本,而不是引用
<?php
$stack
= array ("some" => "value",
array (
"subsome" => "subvalue", array ("subsubsome" => "subsubvalue")),
"some1" => "value1");

$object = new RecursiveArrayIterator ($stack);
$object->next ();
$second_object = &$object->getChildren ();

$second_object->next ();

$third_object = &$second_object->getChildren ();

$third_object->offsetSet ("subsubsome", "subsubdiferent");

var_dump ($object);
var_dump ($second_object);
var_dump ($third_object);
?>

返回

object(RecursiveArrayIterator)#1 (1) {
["storage":"ArrayIterator":private]=>
array(3) {
["some"]=>
string(5) "value"
[0]=>
array(2) {
["subsome"]=>
string(8) "subvalue"
[0]=>
array(1) {
["subsubsome"]=>
string(11) "subsubvalue" <--- 预计会改变
}
}
["some1"]=>
string(6) "value1"
}
}
object(RecursiveArrayIterator)#2 (1) {
["storage":"ArrayIterator":private]=>
array(2) {
["subsome"]=>
string(8) "subvalue"
[0]=>
array(1) {
["subsubsome"]=>
string(11) "subsubvalue" <--- 预计会改变
}
}
}
object(RecursiveArrayIterator)#3 (1) {
["storage":"ArrayIterator":private]=>
array(1) {
["subsubsome"]=>
string(14) "subsubdiferent"
}
}
To Top