PHP 8.6.0 Beta 1 is available for testing

RecursiveArrayIterator::getChildren

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

RecursiveArrayIterator::getChildrenDevuelve un iterador para la entrada actual

Descripción

public function RecursiveArrayIterator::getChildren(): ? RecursiveArrayIterator

Devuelve un iterador para la entrada del iterador actual.

Parámetros

Esta función no contiene ningún parámetro.

Valores devueltos

Un iterador para la entrada actual, si es un array o un object ; o null si ocurre un error.

Errores/Excepciones

Se lanzará una excepción InvalidArgumentException si la entrada actual no contiene un array o un object .

Ejemplos

Ejemplo #1 Ejemplo con RecursiveArrayIterator::getChildren()

<?php
$fruits = array("a" => "lemon", "b" => "orange", array("a" => "apple", "p" => "pear"));
$iterator = new RecursiveArrayIterator($fruits);
while ($iterator->valid()) {
 if ($iterator->hasChildren()) {
 // Muestra todos los hijos
 foreach ($iterator->getChildren() as $key => $value) {
 echo $key . ' : ' . $value . "\n";
 }
 } else {
 echo "Sin hijos.\n";
 }
 $iterator->next();
}
?>

El ejemplo anterior mostrará:

Sin hijos.
Sin hijos.
a : apple
p : pear

Véase también

Found A Problem?

Learn How To Improve This PageSubmit a Pull RequestReport a Bug
+add a note

User Contributed Notes 1 note

up
3
814ckf0x
12 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"
 }
}
+add a note

AltStyle によって変換されたページ (->オリジナル) /