略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: RecursiveArrayIterator::getChildren

2024-11-14

RecursiveArrayIterator::getChildren

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

RecursiveArrayIterator::getChildrenReturns an iterator for the current entry if it is an array or an object

说明

public RecursiveArrayIterator::getChildren(): ?RecursiveArrayIterator

Returns an iterator for the current iterator entry.

参数

此函数没有参数。

返回值

An iterator for the current entry, if it is an array or object; or null on failure.

错误/异常

An InvalidArgumentException will be thrown if the current entry does not contain an array or an object.

范例

示例 #1 RecursiveArrayIterator::getChildren() example

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

$iterator = new RecursiveArrayIterator($fruits);

while (
$iterator->valid()) {

    if (
$iterator->hasChildren()) {
        
// print all children
        
foreach ($iterator->getChildren() as $key => $value) {
            echo 
$key ' : ' $value "\n";
        }
    } else {
        echo 
"No children.\n";
    }

    
$iterator->next();
}
?>

以上例程会输出:

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

参见

add a noteadd a note

User Contributed Notes 1 note

up
3
814ckf0x
8 years ago
RecursiveArrayIterator::getChildrens returns a copy of the children, not a reference:
<?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);
?>

returns:

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" <--- expected to be changed
      }
    }
    ["some1"]=>
    string(6) "value1"
  }
}
object(RecursiveArrayIterator)#2 (1) {
  ["storage":"ArrayIterator":private]=>
  array(2) {
    ["subsome"]=>
    string(8) "subvalue"
    [0]=>
    array(1) {
      ["subsubsome"]=>
      string(11) "subsubvalue" <--- expected to be changed
    }
  }
}
object(RecursiveArrayIterator)#3 (1) {
  ["storage":"ArrayIterator":private]=>
  array(1) {
    ["subsubsome"]=>
    string(14) "subsubdiferent"
  }
}

官方地址:https://www.php.net/manual/en/recursivearrayiterator.getchildren.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3