title: Outputting two dimensional array - CodeIgniter
body: <p>I'm trying to match subcategories with their respective parent categories. For now I just hard-coded in the main categories on the view (Books, Clothes etc) and have a function to get all subcategories, then in the view I can make a while loop to match every subcategory with its main one.</p>
  
  <p>I've managed to get the data I want into an array, which now looks like this:</p>
  
  <pre><code>  array
    1 =&gt; 
      array 
        1 =&gt; string 'Medicine'
        2 =&gt; string 'Fairy tales' 
        3 =&gt; string 'Novels' 
      2 =&gt; 
      array
        44 =&gt; string 'Men's Clothing'
        45 =&gt; string 'Men's Accessories'
        49 =&gt; string 'Women's Swimwear' 
        50 =&gt; string 'Boys' 
        51 =&gt; string 'Girls' 
    3 =&gt; 
      array
        52 =&gt; string 'Perfume Sprays'
        53 =&gt; string 'Roll ons'
        54 =&gt; string 'Deodorants'
        64 =&gt; string 'Miscellaneous'
  </code></pre>
  
  <p>Where 1, 2, 3 are the main category ID's, and the array inside corresponds to subcat_id => subcat_name.</p>
  
  <p>I've gotten this array by the following in my model:</p>
  
  <pre><code>function getsub()
  {
      $this-&gt;db-&gt;select('sub_id,sub_name,cat_id');
      $q = $this-&gt;db-&gt;get('subcategories');
  
      $subcats = array();
  
      foreach($q-&gt;result() as $row)
      {
          $subcats[$row-&gt;cat_id][$row-&gt;sub_id] = $row-&gt;sub_name;
      }
  
      return $subcats;
  }
  </code></pre>
  
  <p>Then in my view, under whatever category I have, I make a while loop to display all subcategories matching that cat_id:</p>
  
  <pre><code>&lt;ul class="dropdown-menu"&gt;
  &lt;?php while($subcats['cat_id'] == 1)?&gt;
  
  &lt;?php{?&gt;
  
    &lt;li&gt;&lt;a href="main/sub_prod/&lt;?php echo $subcats['sub_id'];?&gt;"&gt;
  &lt;?php echo $subcats['sub_name'];?&gt;&lt;/a&gt;&lt;/li&gt;
  
  &lt;?php}?&gt;
  
  &lt;/ul&gt;
  </code></pre>
  
  <p>But I get an error on the line that makes each list item, saying 'undefined index: 'subcat_name'. The same is for subcat_id in that list item. Could someone tell me whats wrong? Thank you</p>