Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / PHP

RecursiveIteratorIterator

Recorre un árbol RecursiveIterator sin escribir una búsqueda recursiva.

Versión: PHP 8.4; repository runtime, production runtime not attested

Sintaxis

new RecursiveIteratorIterator($recursive, RecursiveIteratorIterator::LEAVES_ONLY)

Recorre un árbol RecursiveIterator sin escribir una búsqueda recursiva. LEAVES_ONLY emite hojas, SELF_FIRST el padre antes de los descendientes y CHILD_FIRST después. Las claves son locales y pueden repetirse entre ramas.

$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]]

Ejemplos

<?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);

Resultado

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

Fuentes