CKTIS / PHP
RecursiveIteratorIterator
Parcourt un arbre RecursiveIterator sans boucle récursive écrite à la main.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
new RecursiveIteratorIterator($recursive, RecursiveIteratorIterator::LEAVES_ONLY)Parcourt un arbre RecursiveIterator sans boucle récursive écrite à la main. LEAVES_ONLY émet les feuilles, SELF_FIRST le parent avant ses descendants, CHILD_FIRST après eux. Les clés sont locales et peuvent se répéter dans différentes branches.
$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]]
Exemples
<?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);
Résultat
[[1,"id",1],[1,"id",2]]