title: Getting Modified Preorder Tree Traversal Data into an Array
body: <p>What is an efficient way to put the tree data into an <strong>array</strong>?</p>
  
  <p>I followed the <a href="http://articles.sitepoint.com/article/hierarchical-data-database/2" rel="nofollow noreferrer">sitepoint tutorial</a> to retrieve the tree data.</p>
  
  <p>However, the tutorial only shows how to output the tree, not how to make a multidementional array.</p>
  
  <p>I used </p>
  
  <pre><code>SELECT title, lft, rgt FROM tree_structure WHERE lft BETWEEN $parentLft  AND $parentRgt ORDER BY lft ASC
  </code></pre>
  
  <p>So for each item, I have its title, left and right values.</p>
  
  <p>I am stuck on making the array look like this</p>
  
  <pre><code>Array
  (
   Title: Main Topic
   Children =&gt; Array
               (
                =&gt; Title: subTopic
                       Leaf:  true
                =&gt; Title: Another subtopic
                       Children =&gt;  Array
                                 (
                                  =&gt; Title: subtopic child
                                    Leaf: true
                                 )
                ) 
  
  )
  </code></pre>
  
  <p>If you could help, I would really appreciate it.</p>
  
  <p>PS. The sql output looks <a href="http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/" rel="nofollow noreferrer">like this</a> (except I have title, not name and don't use category_id ):</p>
  
  <pre><code>+-------------+----------------------+-----+-----+
  | category_id | name                 | lft | rgt |
  +-------------+----------------------+-----+-----+
  |           1 | ELECTRONICS          |   1 |  20 |
  |           2 | TELEVISIONS          |   2 |   9 |
  |           3 | TUBE                 |   3 |   4 |
  |           4 | LCD                  |   5 |   6 |
  |           5 | PLASMA               |   7 |   8 |
  |           6 | PORTABLE ELECTRONICS |  10 |  19 |
  |           7 | MP3 PLAYERS          |  11 |  14 |
  |           8 | FLASH                |  12 |  13 |
  |           9 | CD PLAYERS           |  15 |  16 |
  |          10 | 2 WAY RADIOS         |  17 |  18 |
  </code></pre>