title: Creating a single SELECT using multilevel parent->child array
body: <p>I used the following stackoverflow <a href="https://stackoverflow.com/questions/2273449/creating-a-multilevel-array-using-parentids-in-php">thread</a>, which was very helpful, in creating a sorted multilevel parent->child array.</p>
  
  <p>The below is a sample multi-level (3 levels) parent->child sorted array based on the sorting method of the above stated thread:</p>
  
  <pre><code>Array 
  ( 
      [1] =&gt; Array 
          ( 
              [name] =&gt; User Manual 
              [parentId] =&gt; 0 
              [children] =&gt; Array
                  ( 
                      [2] =&gt; Array 
                          ( 
                              [name] =&gt; Users 
                              [parentId] =&gt; 1 
                              [children] =&gt; Array 
                                  ( 
                                      [4] =&gt; Array 
                                          ( 
                                              [name] =&gt; Privileges 
                                              [parentId] =&gt; 2 
                                          ) 
                                  )
                          ) 
                      [3] =&gt; Array 
                          ( 
                              [name] =&gt; Reference 
                              [parentId] =&gt; 1 
                              [children] =&gt; Array 
                                  ( 
                                      [5] =&gt; Array 
                                          ( 
                                              [name] =&gt;  Glossary
                                              [parentId] =&gt; 3 
                                          ) 
                                      [6] =&gt; Array 
                                          ( 
                                              [name] =&gt;  Index
                                              [parentId] =&gt; 3
                                          )
                                  ) 
                          ) 
                  )
          )
  )
  </code></pre>
  
  <p>Now I'd like to take that sorted array and generate a <strong>single</strong> HTML SELECT that displays and represents the parent->child structure like the below example:</p>
  
  <pre><code>&lt;option value='1'&gt;User Manual&lt;/option&gt;
  &lt;option value='2'&gt;User Manual -&gt; Users&lt;/option&gt;
  &lt;option value='4'&gt;User Manual -&gt; Users -&gt; Privileges&lt;/option&gt;
  &lt;option value='3'&gt;User Manual -&gt; Reference&lt;/option&gt;
  &lt;option value='5'&gt;User Manual -&gt; Reference -&gt; Glossary&lt;/option&gt;
  &lt;option value='6'&gt;User Manual -&gt; Reference -&gt; Index&lt;/option&gt;
  </code></pre>
  
  <p>The goal as seen above is to compile a display string that represents the multi-level parent/child path for each item.  The VALUE to each option is the last item in the represented compiled string.  So for the OPTION 'User Manual -> Users -> Privileges' the VALUE is the ID for Privileges, but the display text represents it's parent/child relationship in the array.</p>
  
  <p>The select I create always ends up displaying the list of options like the below:</p>
  
  <pre><code>&lt;option value='1'&gt;User Manual&lt;/option&gt;
  &lt;option value='2'&gt;User Manual -&gt; Users&lt;/option&gt;
  &lt;option value='4'&gt;Users -&gt; Privileges&lt;/option&gt;
  &lt;option value='3'&gt;User Manual -&gt; Reference&lt;/option&gt;
  &lt;option value='5'&gt;Reference -&gt; Glossary&lt;/option&gt;
  &lt;option value='6'&gt;Reference -&gt; Index&lt;/option&gt;
  </code></pre>
  
  <p>You can see that it only is able to keep track of one level of the parent/child relationship.</p>
  
  <p>My function that processes the array to create the selects options list is as follows:</p>
  
  <pre><code>function buildOptions($arr, $target, $parent = NULL) {
    $html = "";
    foreach ( $arr as $key =&gt; $v ) 
    {
      if (array_key_exists('children', $v))
      {
        if ( $key == $target )
          $html .= "&lt;option value='$key' selected&gt;$parent {$v['name']}&lt;/option&gt;\n";
        else
          $html .= "&lt;option value='$key'&gt;$parent {$v['name']}&lt;/option&gt;\n";
  
        $html .= buildOptions($v['children'],$target,$v['name']." &gt; ");
      }
      else if ( $key == $target )
        $html .= "&lt;option value='$key' selected&gt;$parent {$v['name']}&lt;/option&gt;\n";
      else
        $html .= "&lt;option value='$key'&gt;$parent {$v['name']}&lt;/option&gt;\n";
  
    }
  
    return $html;
  }
  </code></pre>
  
  <p>I'm having trouble in keeping track of the previous X parent.name when there is more than one level.  Any help here would be welcomed.  Thank you!</p>