title: php how to loop through a multi dimensional array
body: <p>I am trying to create a dynamic navigation list that has a sublist for each of the items in the list</p>
  
  <p>I have 1 array that contains 12 parent category values and is a straightforward 1 dimensional array. </p>
  
  <p>I am looping through that with a foreach loop to make an unordered list</p>
  
  <p>The problem I am having is that I have an array of subcategories that is a multidimensional array and I need to create a nested list for each of the subcategories that belong to the parent category. </p>
  
  <pre><code>&lt;?php
  //mysql query to get the parent categories  
  $query  = "SELECT `parent` FROM `categories` GROUP BY `parent`";
  $result = mysql_query($query);
  
  while ($row = mysql_fetch_assoc($result)) {
      $cat[] = $row['parent']; //define the parent categories as a variable
  }?&gt;
  
  &lt;div id="navigation"&gt;
      &lt;ul&gt;
          &lt;li&gt;&lt;a href="http://localhost/softwarereviews.com"&gt;Home&lt;/a&gt;&lt;/li&gt;
          &lt;?php
          //loop through the parent categories
          foreach ($cat as $parent) {
  
              //another query to get the child categories that belong to each parent category
              $query = "SELECT * FROM `categories` WHERE `parent` = '$parent'";
              $result = mysql_query($query)
                  or die(mysql_error());
  
              while ($row = mysql_fetch_assoc($result)) {
                  //need 2 results so create a multi - dimensional array 
                  $children[] = array($row['name'] =&gt; $row['cat_label']);
  
              }?&gt;
              &lt;li&gt;&lt;?php echo $parent; ?&gt;&lt;/li&gt;
              &lt;ul&gt;
                  &lt;?php foreach ($children as $key =&gt; $value) { ?&gt;
  
                  &lt;?php foreach ($value as $key =&gt; $value) { ?&gt;
  
                      &lt;li&gt;&lt;a href="&lt;?php echo $value;?&gt;"&gt;&lt;?php echo $key;?&gt;&lt;/a&gt;&lt;li&gt;
  
                  &lt;?php }
              }?&gt;
              &lt;/ul&gt;
              &lt;?php }?&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>What happens at the moment is that the sub list of each category keeps appending the previous lists results, making the each sub list results bigger and bigger.</p>