title: How to build tree structure for comments?
body: <p>I am fetching all of the comments for blog post, also I put them in tree structure by using references with function below:</p>
  
  <pre><code>public function getComments($articleID) {
      $sql = "SELECT * FROM comments WHERE articleid = $articleID";
      $database = DatabaseFactory::getFactory()-&gt;Connect();
      $stmt = $database-&gt;query($sql);  
      $res = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
      if($res!=NULL)
      {
          $references = array();
          $tree = array();
          foreach ($res as $id=&gt; &amp;$node) {
              // Use id as key to make a references to the tree and initialize it with node reference.
              $references[$node['comment_id']] = &amp;$node;
  
              // Add empty array to hold the children/subcategories
              $node['children'] = array();
  
              // Get your root node and add this directly to the tree
              if ($node['comment_respond_to']==0) {
                  $tree[$node['comment_id']] = &amp;$node;
              } else {
                  // Add the non-root node to its parent's references
                  $references[$node['comment_respond_to']]['children'][$node['comment_id']] = &amp;$node;
              }
          }
  
          return $tree;
  </code></pre>
  
  <p>When I <code>print_r($tree);</code> this is the output;</p>
  
  <pre><code>Array
  (
      [1] =&gt; Array
          (
              [comment_id] =&gt; 1
              [articleid] =&gt; 1
              [user_id] =&gt; 2
              [comment_comment] =&gt; First comment.
              [comment_date] =&gt; 2016-03-10 20:06:43
              [comment_respond_to] =&gt; 0
              [children] =&gt; Array
                  (
                      [2] =&gt; Array
                          (
                              [comment_id] =&gt; 2
                              [articleid] =&gt; 1
                              [user_id] =&gt; 1
                              [comment_comment] =&gt; First comment, first child.
                              [comment_date] =&gt; 2016-03-10 20:06:43
                              [comment_respond_to] =&gt; 1
                              [children] =&gt; Array
                                  (
                                  )
                      )
  
                  [4] =&gt; Array
                      (
                          [comment_id] =&gt; 4
                          [articleid] =&gt; 1
                          [user_id] =&gt; 1
                          [comment_comment] =&gt; First comment, second child.
                          [comment_date] =&gt; 2016-03-10 20:06:43
                          [comment_respond_to] =&gt; 1
                          [children] =&gt; Array
                              (
                              )
  
                      )
  
              )
  
      )
  
  [3] =&gt; Array
      (
          [comment_id] =&gt; 3
          [articleid] =&gt; 1
          [user_id] =&gt; 2
          [comment_comment] =&gt; Second comment.
          [comment_date] =&gt; 2016-03-10 20:06:43
          [comment_respond_to] =&gt; 0
          [children] =&gt; Array
              (
                  [6] =&gt; Array
                      (
                          [comment_id] =&gt; 6
                          [articleid] =&gt; 1
                          [user_id] =&gt; 1
                          [comment_comment] =&gt; Second comment, first child.
                          [comment_date] =&gt; 2016-03-10 20:06:43
                          [comment_respond_to] =&gt; 3
                          [children] =&gt; Array
                              (
                              )
  
                      )
  
              )
  
      )
  
  )       
  </code></pre>
  
  <p>Everything goes perfect.</p>
  
  <p>Time to echo out.</p>
  
  <p>My desired output:</p>
  
  <blockquote>
    <ol>
    <li>First Comment
    
    <ol start="2">
    <li>First comment, first child.</li>
    <li>First comment, second child.</li>
    </ol></li>
    <li>Second Comment
    
    <ol start="2">
    <li>Second comment, first child.</li>
    </ol></li>
    </ol>
  </blockquote>
  
  <p>So, I know my next step should be creating loop with foreach and get the comments.</p>
  
  <p>I replace <code>return $tree;</code> with;</p>
  
  <pre><code>        foreach ($tree as $key) {
              echo '&lt;li&gt;'.$key["comment_comment"], '&lt;/li&gt;';
          }
  </code></pre>
  
  <p>It outputs;</p>
  
  <blockquote>
    <ol>
    <li>First Comment</li>
    <li>Second Comment</li>
    </ol>
  </blockquote>
  
  <p>Child comments are missing.
  I know my next step should be creating another foreach inside of current foreach to get the child comments but I don't know how to do it.</p>
  
  <p>I appreciate any help.</p>