title: Format my JSON string into an <ol> ordered list in PHP
body: <p>I'm working on a simple CMS for a pet project. I currently have a JSON string that contains a list of page ID's and Parent page ID's for a menu structure.</p>
  
  <p>I want to now convert this string into a nested or hierarchical list (ordered list).</p>
  
  <p>I've tried looking looping through but seem to have ended up with an overly complex range of sub classes. I'm struggling to find a suitable light-weight solution in PHP.</p>
  
  <p>Here's the JSON:</p>
  
  <pre><code>**[{"id":3,"children":[{"id":4,"children":[{"id":5}]}]},{"id":6},{"id":2},{"id":4}]**
  </code></pre>
  
  <p>Here's the desired output:</p>
  
  <pre><code>&lt;ol&gt;
    &lt;li&gt;3
      &lt;ol&gt;
        &lt;li&gt;4&lt;/li&gt;
           &lt;ol&gt;
              &lt;li&gt;5&lt;/li&gt;
           &lt;/ol&gt;
      &lt;/ol&gt;
    &lt;/li&gt;
    &lt;li&gt;6&lt;/li&gt;
    &lt;li&gt;2&lt;/li&gt;
    &lt;li&gt;4&lt;/li&gt;
  &lt;/ol&gt;
  </code></pre>
  
  <p>Is there anything built in to PHP that can simplify this process? Has anyone had any experience of this before?</p>
  
  <p>I'm a newbie to PHP and SE. Looking forward to hearing from you.</p>
  
  <p><strong>Here's my current progress (it's not working too well)</strong></p>
  
  <pre><code>&lt;ol&gt;
  &lt;?php
  $json = '[{"id":3,"children":[{"id":4,"children":[{"id":5}]}]},{"id":6},{"id":2},{"id":4}]';
  $decoded = json_decode($json);
  $pages = $decoded;
  foreach($pages as $page){
     $subpages = $decoded-&gt;children;
        echo "&lt;li&gt;".$page-&gt;id."&lt;/li&gt;";
      foreach($subpages as $subpage){
          echo "&lt;li&gt;".$subpage-&gt;id."&lt;/li&gt;";
     }
  }
  ?&gt;
  &lt;/ol&gt;
  </code></pre>