title: How to print list using hierarchical data structure?
body: <p>When I run this code:</p>
  
  <pre><code>foreach ($tree as $node) {
      echo str_repeat(' ', $node-&gt;tree_depth * 4) . $node-&gt;id . PHP_EOL;
  }
  </code></pre>
  
  <p>I get well formatted text like:</p>
  
  <pre><code>Food
   Fruit
     Red
       Cherry
       Strawberry
                 Cool
                 Not cool
     Yellow
       Banana
   Meat
     Beef
     Pork
  </code></pre>
  
  <p>But I want to create a list with <code>&lt;ul&gt;&lt;li&gt;...</code>:</p>
  
  <p>I tried with:</p>
  
  <pre><code>echo '&lt;ul&gt;';
  $prev_depth = 0;
  foreach($table-&gt;fetchTree() as $row) {
      if ($row-&gt;tree_depth &gt; $prev_depth) {
          echo '&lt;li&gt;&lt;ul&gt;';
      } else if ($row-&gt;tree_depth &lt; $prev_depth) {
          echo '&lt;/li&gt;&lt;/ul&gt;';
      }
      echo '&lt;li&gt;' . $row-&gt;name . '&lt;/li&gt;';
      $prev_depth = $row-&gt;tree_depth;
  }
  echo '&lt;/ul&gt;';
  </code></pre>
  
  <p>But I have some extra ul tags and so on.  I lost 2 days on this so if you can help me please post here...</p>