title: PHP foreach create menu from array
body: <p>I am currently creating a joomla component, and i am currently stuck with the menu part.</p>
  
  <p>this is in my model file:</p>
  
  <pre><code>public function getMenu(){
      $menu_id = JRequest::getInt('id');
  
      $db = $this-&gt;getDbo();
      $query = $db-&gt;getQuery(true);
  
      $query-&gt;select('t.country_name,t.country_code, a.continent_name');
      $query-&gt;from('#__vectormap_countries AS t')
              -&gt;join('LEFT', '#__vectormap_continents AS a USING(continent_id)')
              -&gt;where('t.published = 1');
  
      $db-&gt;setQuery($query);
  
      $menu_items = $db-&gt;loadObjectList();
  
      return $menu_items;
  }
  </code></pre>
  
  <p>and on the front end i have:</p>
  
  <pre><code>&lt;?php  $menus = $this-&gt;menu ?&gt;
  &lt;?php foreach($menus as $menu): ?&gt;
      &lt;div&gt;&lt;?php echo $menu-&gt;continent_name ?&gt;&lt;/div&gt;
      &lt;li id="&lt;?php echo $menu-&gt;country_code ?&gt;"&gt;&lt;?php echo $menu-&gt;country_name ?&gt;&lt;/li&gt;
      &lt;br /&gt;
  &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>and that returns:</p>
  
  <p>Africa
  South Africa</p>
  
  <p>Africa
  Mozambique
  or if i print out the array this:</p>
  
  <pre><code>Array ( [0] =&gt; stdClass Object ( [country_name] =&gt; South Africa [country_code] =&gt; ZA [continent_name] =&gt; Africa ) [1] =&gt; stdClass Object ( [country_name] =&gt; Mozambique [country_code] =&gt; MZ [continent_name] =&gt; Africa ) ) 1 
  </code></pre>
  
  <p>Now Finally the question, how would i sort it so that Africa (<code>continent_name</code>) not be repeated but rather have all the countries that have the continent_name of Africa list underneath it?</p>
  
  <p>Keep in mind North America and such will also come in to play.. </p>
  
  <p>Summarized question -> How would I sort countries underneath there associated continents fond in the array.</p>
  
  <p>Any Help Greatly Appreciated thanks.</p>
  
  <p>how would i sort it that everything </p>