title: Re-structure n-level array with parent/child relation (PHP)
body: <p>I want to restructure an array and some of the solutions on stuckoverflow helped me to make it "proper" for the 1-st level items, but you'll notice the array is n-level deep.</p>
  
  <p>The method <strong>restructure()</strong> is not used recursively (it should be). It's probably wrong alltogether, and have no idea how to make it right.</p>
  
  <p>The <em>children</em> key says there are children with the respective id's, <em>parent</em> key links the item to the parent id.</p>
  
  <pre><code>class FilterMenu {
  
      protected $tree = array();
  
      static protected $structure = array();
  
      public function __construct(array $tree)
      {
          $this-&gt;tree = $tree;
      }
  
      public function getStructure()
      {
          self::restructure($this-&gt;tree);
          return self::$structure;
      }
  
      static public function restructure(array $structure)
      {
          foreach ($structure as $k =&gt; $v)
          {
              if (isset($v['parent']) and isset($v['children']) and count($v['children']) == 1)
              {
                  // only 1 child
                  self::$structure[$k] = current(array_keys($v['children']));
              }
              elseif (isset($v['children']))
              {
                  $keys = array_keys($v['children']);
                  self::$structure[$k] = array_combine($keys, $keys); // mirror array
  
                  //self::restructure($v['children']);
              }
              else
              {
                  // no children
                  self::$structure[$k] = $k;
              }
          }
      }
  }
  
  
  
  // test array
  
  $tree = array(
      1 =&gt; array(
          'parent' =&gt; 1
      ),
      2 =&gt; array(
          'parent' =&gt; 2,
          'children' =&gt; array(
              3 =&gt; array(
                  'parent' =&gt; 2
              ),
              6 =&gt; array(
                  'parent' =&gt; 2,
                  'children' =&gt; array(
                      10 =&gt; array(
                          'parent' =&gt; 6,
                          'children' =&gt; array(
                              4 =&gt; array(
                                  'parent' =&gt; 10
                              )
                          )
                      )
                  ),
              ),
          ),
      ),
      7 =&gt; array(
          'parent' =&gt; 7,
          'children' =&gt; array(
              11 =&gt; array(
                  'parent' =&gt; 7
              )
          )
      ),
      14 =&gt; array(
          'parent' =&gt; 14,
          'children' =&gt; array(
              15 =&gt; array(
                  'parent' =&gt; 14,
              ),
              16 =&gt; array(
                  'parent' =&gt; 14,
              ),
              19 =&gt; array(
                  'parent' =&gt; 14,
              ),
              20 =&gt; array(
                  'parent' =&gt; 14,
              ),
              21 =&gt; array(
                  'parent' =&gt; 14,
              ),
          )
      )
  );
  
  // test:
  $tree = new FilterMenu($tree);
  echo '&lt;pre&gt;'.print_r($tree-&gt;getStructure(), true);
  </code></pre>
  
  <p>Actual result:</p>
  
  <pre><code>Array
  (
      [1] =&gt; 1
      [2] =&gt; Array
          (
              [3] =&gt; 3
              [6] =&gt; 6
          )
  
      [7] =&gt; 11
      [14] =&gt; Array
          (
              [15] =&gt; 15
              [16] =&gt; 16
              [19] =&gt; 19
              [20] =&gt; 20
              [21] =&gt; 21
          )
  
  )
  </code></pre>
  
  <p>the desired/expected result is:</p>
  
  <pre><code>Array
  (
      [1] =&gt; 1
      [2] =&gt; Array
          (
              [3] =&gt; 3
              [6] =&gt; Array
                  (
                      [10] =&gt; 4 // &lt;-- array with n-levels...
                  )
  
          )
  
      [7] =&gt; 11
      [14] =&gt; Array
          (
              [15] =&gt; 15
              [16] =&gt; 16
              [19] =&gt; 19
              [20] =&gt; 20
              [21] =&gt; 21
          )
  
  )
  </code></pre>
  
  <p>Here is the <a href="http://codepad.org/Gag5Ullb#output" rel="noreferrer">link to codepad</a> that tests the class and the array.</p>
  
  <p>Any help is greatly appreciated. Thanks.</p>