title: A custom PHP function to recursively iterate over directories and output a hierarchical, multidimensional array?
body: <p>A custom PHP function to recursively iterate over directories and output a hierarchical, multidimensional array?</p>
  
  <h2>Using the new SPL Iterator class (RecursiveIterator*), I've been working on the following function:</h2>
  
  <pre><code>function directoryToArray( $directory ) {
  
      $array = [];
  
      $objects = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $directory ), RecursiveIteratorIterator::SELF_FIRST );
  
      foreach ( $objects as $name =&gt; $object ) {
  
          if ( !( $object-&gt;getFilename() == "." | $object-&gt;getFilename() == ".." ) ) {
              $path = $object-&gt;isDir() ? [
                      [
                          'name' =&gt; $object-&gt;getFilename(),
                          'file' =&gt; $object-&gt;getFilename(),
                          'children' =&gt; []
                      ]
              ] : [
                  [
                      'name' =&gt; friendlyName( $object-&gt;getFilename() ),
                      'file' =&gt; $object-&gt;getFilename(),
                  ]
              ];
  
              for ( $depth = $objects-&gt;getDepth() - 1; $depth &gt;= 0; $depth-- ) {
                  $path = [
                      $objects-&gt;getSubIterator( $depth )-&gt;current()-&gt;getFilename() =&gt; $path,
                  ];
              }
  
              $array = array_merge_recursive( $array, $path );
  
          }
  
      }
      return $array;
  }
  </code></pre>
  
  <p>This currently results in the following output:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [name] =&gt; Anchor Links
              [file] =&gt; anchor-links.php
          )
  
      [1] =&gt; Array
          (
              [name] =&gt; Columns
              [file] =&gt; columns.php
          )
  
      [2] =&gt; Array
          (
              [name] =&gt; Page Layouts
              [file] =&gt; page-layouts
              [children] =&gt; Array
                  (
                  )
  
          )
  
      [page-layouts] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [name] =&gt; Right Sidebar
                      [file] =&gt; right-sidebar.php
                  )
  
              [1] =&gt; Array
                  (
                      [name] =&gt; Left Sidebar
                      [file] =&gt; left-sidebar.php
                  )
  
              [2] =&gt; Array
                  (
                      [name] =&gt; Right Sidebar
                      [file] =&gt; right-sidebar
                      [children] =&gt; Array
                          (
                          )
  
                  )
  
              [right-sidebar] =&gt; Array
                  (
                      [0] =&gt; Array
                          (
                              [name] =&gt; Other Options
                              [file] =&gt; other-options.php
                          )
  
                      [1] =&gt; Array
                          (
                              [name] =&gt; Option A
                              [file] =&gt; option-a.php
                          )
  
                      [2] =&gt; Array
                          (
                              [name] =&gt; Other Options
                              [file] =&gt; other-options
                              [children] =&gt; Array
                                  (
                                  )
  
                          )
  
                      [other-options] =&gt; Array
                          (
                              [0] =&gt; Array
                                  (
                                      [name] =&gt; Sample
                                      [file] =&gt; sample.php
                                  )
  
                          )
  
                  )
  
              [3] =&gt; Array
                  (
                      [name] =&gt; Changelog
                      [file] =&gt; changelog.php
                  )
  
          )
  
  )
  </code></pre>
  
  <p>However, the output I'm trying to achieve is the following:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [name] =&gt; Anchor Links
              [file] =&gt; anchor-links.php
          )
  
      [1] =&gt; Array
          (
              [name] =&gt; Columns
              [file] =&gt; columns.php
          )
  
      [2] =&gt; Array
          (
              [name] =&gt; Page Layouts
              [file] =&gt; page-layouts
              [children] =&gt; Array
                  (
  
                      [0] =&gt; Array
                          (
                              [name] =&gt; Right Sidebar
                              [file] =&gt; right-sidebar.php
                          )
  
                      [1] =&gt; Array
                          (
                              [name] =&gt; Left Sidebar
                              [file] =&gt; left-sidebar.php
                          )
  
                      [2] =&gt; Array
                          (
                              [name] =&gt; Right Sidebar
                              [file] =&gt; right-sidebar
                              [children] =&gt; Array
                                  (
                                      [0] =&gt; Array
                                          (
                                              [name] =&gt; Other Options
                                              [file] =&gt; other-options.php
                                          )
  
                                      [1] =&gt; Array
                                          (
                                              [name] =&gt; Option A
                                              [file] =&gt; option-a.php
                                          )
  
                                      [2] =&gt; Array
                                          (
                                              [name] =&gt; Other Options
                                              [file] =&gt; other-options
                                              [children] =&gt; Array
                                                  (
                                                      [0] =&gt; Array
                                                          (
                                                              [name] =&gt; Sample
                                                              [file] =&gt; sample.php
                                                          )
                                                  )
  
                                          )
  
                                  )
  
                          )
  
                      [3] =&gt; Array
                          (
                              [name] =&gt; Changelog
                              [file] =&gt; changelog.php
                          )
  
                  )
  
          )
  )
  </code></pre>
  
  <p>I think we're almost there, I'm just stumped on how to get the subdirectories to output to the (now) empty array inside of [children]. Also, do not be confused about the duplicate or similar names (e.g., there is a right-sidebar.php file and a right-sidebar subdirectory in the same directory).</p>