title: How to generate a PHP array from MySQL database record for multi-level folder structure
body: <p>I am wanting to build an Evernote style note app with PHP and MySQL.</p>
  
  <p>Evernote allows top level folders with note records in them.</p>
  
  <p>I would like to make my app allow folders to be inside of folders so a top level folder can have a child folder which then has note records inside of the child folder.</p>
  
  <p>Like this image below...</p>
  
  <p><a href="https://i.stack.imgur.com/PJJEo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PJJEo.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>I have this PHP function which will build the structure menu shown in the image above...</p>
  
  <p>This code build the menu shown   </p>
  
  <pre><code>&lt;?php
  function tree($array, $parent, $parts = array(), $step = 0) {
  
      if (!count($array)) {
          return '';
      }
  
      $tid = ($step == 0) ? 'id="tree"' : '';
      $t = '&lt;ul class="unstyled" '.$tid.'&gt;';
  
      foreach ($array as $key =&gt; $item) {
          if (is_array($item)) {
              $open = $step !== false &amp;&amp; (isset($parts[$step]) &amp;&amp; $key == $parts[$step]);
  
              $t .= '&lt;li class="directory'. ($open ? ' open' : '') .'"&gt;';
                  $t .= '&lt;a href="#" data-role="directory"&gt;&lt;i class="glyphicon glyphicon-folder-'. ($open ? 'open' : 'close') .'"&gt;&lt;/i&gt;&amp;nbsp; ' . $key . '&lt;/a&gt;';
                  $t .= tree($item, "$parent/$key", $parts, $open ? $step + 1 : false);
              $t .=  '&lt;/li&gt;';
          } else {
              $selected = (isset($parts[$step]) &amp;&amp; $item == $parts[$step]);
              $t .= '&lt;li class="file'. ($selected ? ' active' : '') .'"&gt;&lt;a href="'. $parent .'/'. $item . '"&gt;'.$item.'&lt;/a&gt;&lt;/li&gt;';
          }
      }
  
      $t .= '&lt;/ul&gt;';
  
      return $t;
  }
  ?&gt;
  </code></pre>
  
  <hr>
  
  <p>The code above build the tree menu by calling <code>_getTree()</code></p>
  
  <pre><code>protected function _getTree($dir = LIBRARY)
  {
      $return = array('directories' =&gt; array(), 'files' =&gt; array());
  
      $items = scandir($dir);
      foreach ($items as $item) {
          if(preg_match($this-&gt;_ignore, $item)) {
              if($this-&gt;_force_unignore === false || !preg_match($this-&gt;_force_unignore, $item)) {
                  continue;
              }
          }
  
          $path = $dir . DIRECTORY_SEPARATOR . $item;
          if (is_dir($path)) {
              $return['directories'][$item] = $this-&gt;_getTree($path);
              continue;
          }
  
          $return['files'][$item] = $item;
      }
  
      uksort($return['directories'], "strnatcasecmp");
      uksort($return['files'], "strnatcasecmp");
  
      return $return['directories'] + $return['files'];
  }
  </code></pre>
  
  <hr>
  
  <p><code>_getTree()</code> above builds an array in the format shown below by scanning directories on server looking for folders and files.  It then passes that array into <code>tree()</code>.  My goal is to make a function build this same style array but from MySQL Database folder and note records.</p>
  
  <p>Can anyone help me with a function that could build array like below from 2 database tables.</p>
  
  <p>MY database tables will be </p>
  
  <ul>
  <li><strong>notebooks</strong> with column name <code>parent</code> to denote the parent folder.  THe notebook ID will go into parent field if the notebook is a child folder of a parent notebook.  0 == parent level folder.</li>
  <li><strong>notes</strong> with <code>parent</code> column to be linked to ID of notebook.</li>
  </ul>
  
  <p>Any help greatly appreciated? </p>
  
  <pre><code>Array(
      [top_level_notebook_1] =&gt; Array(
              [child_note_of_parent_notebook_1.html] =&gt; some_note.html
              [child_level_notebook_1] =&gt; Array(
                      [note_1.html] =&gt; some_note.html
              )
  
              [child_level_notebook_2] =&gt; Array(
                      [note_2] =&gt; cache.js
                      [note_3] =&gt; cache.js.md
                      [note_4] =&gt; Confirmation-Dialog.js
              )
  
              [child_level_notebook_3] =&gt; Array(
                      [crud.php] =&gt; crud.php
                      [error2mysql.php] =&gt; error2mysql.php
                      [upload_file_from_remote_url.php] =&gt; upload_file_from_remote_url.php
              )
  
      )
  
      [top_level_notebook_2] =&gt; Array(
              [note_7.html] =&gt; some_note.html
      )
  
      [root_level_note.html] =&gt; Dev_Bookmarks.html
  )
  </code></pre>