Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursiveIteratorIterator and nested RecursiveArrayIterators
    primarykey
    data
    text
    <p>I've been messing with RecursiveArrayIterators for handling nested objects as trees. The following code is bothering me as the result is returning only some of the values I'm expecting. Mainly, the root node seems to never be iterated over. I have a feeling I've just been staring at it a bit too long but wanted to make sure I was on the right track with this.</p> <pre><code>class Container extends \RecursiveArrayIterator { protected $_alias; public function __construct( $alias = null ) { if( is_null( $alias ) ) { $alias = uniqid( 'block_' ); } $this-&gt;_alias = $alias; } public function getAlias() { return $this-&gt;_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-&gt;append( $child_of_child_a ); $child-&gt;append( $child_of_child_b ); $child_of_child_a-&gt;append( new Container ); $child_of_child_a-&gt;append( new Container ); $block-&gt;append( $child ); $root-&gt;append( $block ); $storage = new \RecursiveIteratorIterator( $root, RecursiveIteratorIterator::SELF_FIRST ); foreach( $storage as $key =&gt; $value ) { print_r( str_repeat( ' ', $storage-&gt;getDepth() * 4 ) . $value-&gt;getAlias() . PHP_EOL ); } } catch( \Exception $e ) { var_dump( $e-&gt;getMessage() ); } </code></pre> <p>With the result being...</p> <pre><code>first_level second_level third_level_a block_51f98b779c107 block_51f98b779c124 third_level_b </code></pre> <p>Where's the root node?</p> <h2>ANSWER</h2> <p>Sven's answer jarred my over-worked brain into processing this correctly. This was my final bit of successful code in case someone else is attempting something similar.</p> <pre><code>class OuterContainer extends \ArrayIterator { } class Container extends \ArrayIterator { protected $_alias; public function __construct( $alias = null ) { if( is_null( $alias ) ) { $alias = uniqid( 'container_' ); } $this-&gt;_alias = $alias; } public function getAlias() { return $this-&gt;_alias; } } try { $c1 = new Container( 'Base' ); $c1_c1 = new Container( 'Base &gt; 1st Child' ); $c1_c2 = new Container( 'Base &gt; 2nd Child' ); $c1_c1_c1 = new Container( 'Base &gt; 1st Child &gt; 1st Child' ); $c1_c1-&gt;append( $c1_c1_c1 ); $c1-&gt;append( $c1_c1 ); $c1-&gt;append( $c1_c2 ); $outer_container = new OuterContainer; $outer_container-&gt;append( $c1 ); $storage = new \RecursiveIteratorIterator( new \RecursiveArrayIterator( $outer_container ), RecursiveIteratorIterator::SELF_FIRST ); foreach( $storage as $key =&gt; $value ) { print_r( $value-&gt;getAlias() . PHP_EOL ); } } catch( \Exception $e ) { var_dump( $e-&gt;getMessage() ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload