Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that assigning an array does not copy references, but makes a copy of the array. This means, that the "Second Child" array you have in "children" of the "Parent" node is not the same array that you add "Grandchild" to, but a copy of it.</p> <p>To resolve the issue, you have to explicitly use reference assignment instead of copying:</p> <pre><code>function projectListToTree($projects) { $stack = Array(); for($x =0; $x &lt; count($projects); $x++) { $project = &amp;$projects[$x]; $project['Children'] = array(); while(count($stack) &gt; 0 &amp;&amp; $stack[count($stack) - 1]['Right'] &lt; $project['Right']) { array_pop($stack); } if(count($stack) &gt; 0) { $stack[count($stack) - 1]['Children'][] = &amp;$project; echo "Adding " . $project['Name'] . " to " . $stack[count($stack) - 1]['Name'] . " for a total of " . count($stack[count($stack) - 1]['Children']) . " kids\n"; echo "\n"; } else { echo "No parent\n"; } echo "stack count: " . count($stack) . "\n"; $stack[] = &amp;$project; } echo "Left in stack: " . count($stack) . "\n"; return $stack[0]; } </code></pre> <p>Note, that an ampersand was added at three places.</p> <p>Because of this issue, one has to be extremely careful when using nested arrays and assignment operators in php.</p> <p>This also means that processor usage and memory footprint is intensive when using lots of data in nested arrays. For example, in the above example, when projectListToTree() returns, the complete array tree is copied to the local variable $tree and (since the php garbage collector sucks) is in memory twice.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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