php - RecursiveIteratorIterator and nested RecursiveArrayIterators -


i've been messing recursivearrayiterators handling nested objects trees. following code bothering me result returning of values i'm expecting. mainly, root node seems never iterated over. have feeling i've been staring @ bit long wanted make sure on right track this.

class container extends \recursivearrayiterator {     protected $_alias;      public function __construct( $alias = null )     {         if( is_null( $alias ) )         {             $alias = uniqid( 'block_' );         }          $this->_alias = $alias;     }      public function getalias()     {         return $this->_alias;     } }  try {     $root = new container( 'root_level' );     $block = new container( 'first_level' );     $child = new container( 'second_level' );     $child_of_child_a = new container( 'third_level_a' );     $child_of_child_b = new container( 'third_level_b' );      $child->append( $child_of_child_a );     $child->append( $child_of_child_b );     $child_of_child_a->append( new container );     $child_of_child_a->append( new container );     $block->append( $child );     $root->append( $block );      $storage = new \recursiveiteratoriterator( $root, recursiveiteratoriterator::self_first );      foreach( $storage $key => $value )     {         print_r( str_repeat( ' ', $storage->getdepth() * 4 ) . $value->getalias() . php_eol );     } } catch( \exception $e ) {     var_dump( $e->getmessage() ); } 

with result being...

first_level second_level     third_level_a         block_51f98b779c107         block_51f98b779c124     third_level_b 

where's root node?

answer

sven's answer jarred over-worked brain processing correctly. final bit of successful code in case else attempting similar.

class outercontainer extends \arrayiterator {  }  class container extends \arrayiterator {     protected $_alias;      public function __construct( $alias = null )     {         if( is_null( $alias ) )         {             $alias = uniqid( 'container_' );         }          $this->_alias = $alias;     }      public function getalias()     {         return $this->_alias;     } }  try {     $c1 = new container( 'base' );      $c1_c1 = new container( 'base > 1st child' );      $c1_c2 = new container( 'base > 2nd child' );      $c1_c1_c1 = new container( 'base > 1st child > 1st child' );      $c1_c1->append( $c1_c1_c1 );      $c1->append( $c1_c1 );      $c1->append( $c1_c2 );      $outer_container = new outercontainer;      $outer_container->append( $c1 );      $storage = new \recursiveiteratoriterator( new \recursivearrayiterator( $outer_container ), recursiveiteratoriterator::self_first );      foreach( $storage $key => $value )     {         print_r( $value->getalias() . php_eol );     } } catch( \exception $e ) {     var_dump( $e->getmessage() ); } 

the root node not there because not using recursivearrayiterator correctly.

it's intended use have array multiple sub-arrays in level , structure, , give array 1 instance of recursivearrayiterator, , iterate on everything, put recursiveiteratoriterator.

that way, entire array, including it's top level, iterated.

because abused recursivearrayiterator carrier of information, top level object not iterated.

suggested easy example has nastyness, demonstrates principle:

$root = array( 'root_level' ); $block = array( 'first_level' ); $child = array( 'second_level' ); $child_of_child_a = array( 'third_level_a' ); $child_of_child_b = array( 'third_level_b' );  $child[] = $child_of_child_a; $child[] = $child_of_child_b ; $child_of_child_a[] = array(''); $child_of_child_a[] = array(''); $block[] = $child; $root[] = $block ;  $storage = new \recursiveiteratoriterator( new \recursivearrayiterator($root), recursiveiteratoriterator::self_first );  var_dump($root); foreach ($storage $key=>$value) {     echo $key.": ".$value."\n"; } 

result output:

array(2) {     [0] =>   string(10) "root_level"   [1] =>   array(2) {         [0] =>     string(11) "first_level"     [1] =>     array(3) {             [0] =>       string(12) "second_level"       [1] =>       array(1) {                 ...             }       [2] =>       array(1) {                 ...             }     }   } } 0: root_level 1: array     0: first_level 1: array     0: second_level 1: array     0: third_level_a 2: array     0: third_level_b 

it's output not quite same, point can iterate on structure without having every single node instance of recursivearrayiterator. can add either array, or object acting array, or object can usefully iterated.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -