group mysql results in nested array
<p>I have a MySQL query like this: </p>

<pre><code>SELECT c.id
     , c.company_name
     , b.brand_id
     , b.brand_name 
  FROM companies as c 
  JOIN brands AS b 
    ON c.id = b.company_id;
</code></pre>

<p>which returns a table like this:</p>

<p><a href="https://i.stack.imgur.com/7p7BO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7p7BO.png" alt="mysql results"></a></p>

<p>The results are stored in <code>$items</code></p>

<p>I want to be able to use a <code>foreach</code> loop to display the results like this:</p>

<pre><code>&lt;ul&gt;
  &lt;li&gt;&lt;b&gt;adidas plc&lt;/b&gt;&lt;/li&gt;
  &lt;li&gt;adidas&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;b&gt;coca-cola&lt;/b&gt;&lt;/li&gt;
  &lt;li&gt;coke&lt;/li&gt;
  &lt;li&gt;dr pepper&lt;/li&gt;
  &lt;li&gt;7up&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;b&gt;ebay&lt;/b&gt;&lt;/li&gt;
  &lt;li&gt;Duobam&lt;/li&gt;
&lt;/ul&gt;
</code></pre>

<p>How can I accomplish this with just using the above join query? I can do it by first querying the company table and then doing a new query for each on the brand table but with thousands of companies and multiple brands for each its going to cause too many queries.</p>

<pre><code>          foreach($items as $item){
            echo '
            &lt;ul&gt;
              &lt;li&gt;&lt;b&gt;'.$item['company_name'].'&lt;/b&gt;&lt;/li&gt;
              &lt;li&gt;'.$item['brand_name'].'&lt;/li&gt;
            &lt;/ul&gt;
            ';
          }
</code></pre>