title: Generating A Select With Multiple Nesting
body: <p>I have multiple nested product categories and I need these in a select box for selection in my admin system.I am using the following function to generate a multidimensional array which can then be passed to codeigniter's <a href="https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html" rel="nofollow">form_dropdown function</a>. This will accept an array within an array, but not a further array inside. If the item in the top level of an array is an array, it will generate an optgroup element instead of a select option. Look at this code for a further explaination:</p>
  
  <pre><code>$categories = array(
      0 =&gt; "top level 1",                 //Generates an &lt;option&gt; element correctly
      "sub level name" =&gt; array (         //Generates an &lt;optgroup&gt; element correctly
          2 =&gt; "sub level 1",
          "sub sub level name" = array(   //Generates &lt;option value="sub sub level name"&gt;Array&lt;/option&gt;
              3 =&gt; "sub sub level 1"
          )
      )
  );
  </code></pre>
  
  <p>To avoid the sub sub level generating an incorrect option element, I need it to generate a sub level with 4 &amp;nbsp's before it, like so:</p>
  
  <pre><code>     $categories = array(
          0 =&gt; "top level 1",                 //Generates an &lt;option&gt; element correctly
          "sub level name" =&gt; array (         //Generates an &lt;optgroup&gt; element correctly
              2 =&gt; "sub level 1"
          ),
         "&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sub sub level name" = array(    //Generates an &lt;optgroup&gt; element correctly
                  3 =&gt; "sub sub level 1"      //Generates an &lt;option&gt; element correctly
              )
      );
  </code></pre>
  
  <p>I can't work out how to do this. I think my code is 95% of the way there as it will generate the first code snippet, but I cannot get to the second code snippet. Here is my function:</p>
  
  <pre><code>function createCategoryOptions($categories, $spacing = "", $top = true)
  {
      print_r($categories);
      $options = array();
      foreach ($categories as $key=&gt;&amp;$category) 
      {
          if (array_key_exists('children', $category))
          {
              usort($category['children'], function($a, $b) {
                  return count($b) - count($a);
              } );
              if($top)
              {
                  $options[$spacing.$category['product_category_title']] = createCategoryOptions($category['children'], $spacing."&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;", false);
              }
              else
              {
                  //This is what generates sub sub levels incorrectly
                  $options[$spacing.$category['product_category_title']] = createCategoryOptions($category['children'], $spacing."&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;", false);
              }
          }
          else
          {
              $options[$key] = $spacing.$category['product_category_title'];
          }
      }
  
      return $options;
  }
  </code></pre>
  
  <p>And here is some sample data which gets input into the function: </p>
  
  <pre><code>array
  (
      //Top Level
      [14] =&gt; array
          (
              [product_category_id] =&gt; 14
              [product_category_parent_id] =&gt; 
              [product_category_title] =&gt; Daily Living
              [children] =&gt; array
                  (
                      //Sub Level
                      [58] =&gt; array
                          (
                              [product_category_id] =&gt; 58
                              [product_category_parent_id] =&gt; 14
                              [product_category_title] =&gt; Daily Living : AJ Way
                              [children] =&gt; array
                                  (
                                      //Sub Sub level
                                      [68] =&gt; array
                                          (
                                              [product_category_id] =&gt; 68
                                              [product_category_parent_id] =&gt; 58
                                              [product_category_title] =&gt; Daily Living : AJ Way : Coverdry
                                          )
  
                                  )
  
                          )
  
                  )
  
          )
  
  )
  </code></pre>
  
  <p>Any ideas?</p>