Base di conoscenzaCentro esami
ITItaliano
Scegli la lingua13

CKTIS / PHP

RecursiveIteratorIterator

Percorre un albero RecursiveIterator senza scrivere un ciclo ricorsivo.

Versione: PHP 8.4; repository runtime, production runtime not attested

Sintassi

new RecursiveIteratorIterator($recursive, RecursiveIteratorIterator::LEAVES_ONLY)

Percorre un albero RecursiveIterator senza scrivere un ciclo ricorsivo. LEAVES_ONLY emette foglie, SELF_FIRST il genitore prima dei discendenti, CHILD_FIRST dopo. Le chiavi sono locali e possono ripetersi in rami diversi.

$tree=new RecursiveArrayIterator(["a"=>["id"=>1],"b"=>["id"=>2]]);
$walk=new RecursiveIteratorIterator($tree); $rows=[];
foreach($walk as $k=>$v){$rows[]=[$walk->getDepth(),$k,$v];} echo json_encode($rows);
[[1,"id",1],[1,"id",2]]

Esempi

<?php
$tree=new RecursiveArrayIterator(["a"=>["id"=>1],"b"=>["id"=>2]]);
$walk=new RecursiveIteratorIterator($tree); $rows=[];
foreach($walk as $k=>$v){$rows[]=[$walk->getDepth(),$k,$v];} echo json_encode($rows);

Risultato

[[1,"id",1],[1,"id",2]]

Fonti