RecursiveArrayIterator::getChildren

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

RecursiveArrayIterator::getChildrenRetorna um iterador para a entrada atual se ela for um array ou um object

Descrição

public RecursiveArrayIterator::getChildren(): ?RecursiveArrayIterator

Retorna um iterador para a entrada atual do iterador.

Parâmetros

Esta função não possui parâmetros.

Valor Retornado

Um iterador para a entrada atual, se ela for um array ou um object; ou null em caso de falha.

Erros/Exceções

Uma InvalidArgumentException será lançada se a entrada atual não contiver um array ou um object.

Exemplos

Exemplo #1 Exemplo de RecursiveArrayIterator::getChildren()

<?php
$fruits
= array("a" => "limão", "b" => "laranja", array("a" => "maçã", "p" => "pêra"));

$iterator = new RecursiveArrayIterator($fruits);

while (
$iterator->valid()) {

if (
$iterator->hasChildren()) {
// imprime todos os filhos
foreach ($iterator->getChildren() as $key => $value) {
echo
$key . ' : ' . $value . "\n";
}
} else {
echo
"Sem filhos.\n";
}

$iterator->next();
}
?>

O exemplo acima produzirá:

Sem filhos.
Sem filhos.
a : maçã
p : pêra

Veja Também

add a note

User Contributed Notes 1 note

up
3
814ckf0x
10 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"
}
}
To Top