title: mysql query help needed - Group by parent
body: <p>I'm trying to do a mysql query that will create an array of parent items, with their child items underneath. But I'm not 100% sure how. Here's what I have done so far:</p>
  
  <p>SELECT * FROM categories as rf ORDER BY parent, name ASC</p>
  
  <p>And here's what is being outputted (array):</p>
  
  <pre><code>Array
  (
      [0] =&gt; stdClass Object
          (
              [id] =&gt; 7
              [name] =&gt; Safety Clothing
              [parent] =&gt; 0
          )
  
      [1] =&gt; stdClass Object
          (
              [id] =&gt; 8
              [name] =&gt; Safety Footwear
              [parent] =&gt; 0
          )
  
      [2] =&gt; stdClass Object
          (
              [id] =&gt; 9
              [name] =&gt; Workwear
              [parent] =&gt; 0
          )
  
      [3] =&gt; stdClass Object
          (
              [id] =&gt; 4
              [name] =&gt; Polos
              [parent] =&gt; 7
          )
  
      [4] =&gt; stdClass Object
          (
              [id] =&gt; 3
              [name] =&gt; Shirts
              [parent] =&gt; 7
          )
  
      [5] =&gt; stdClass Object
          (
              [id] =&gt; 6
              [name] =&gt; Jackets
              [parent] =&gt; 9
          )
  
      [6] =&gt; stdClass Object
          (
              [id] =&gt; 1
              [name] =&gt; Pants
              [parent] =&gt; 9
          )
  
      [7] =&gt; stdClass Object
          (
              [id] =&gt; 2
              [name] =&gt; Shirts
              [parent] =&gt; 9
          )
  
      [8] =&gt; stdClass Object
          (
              [id] =&gt; 5
              [name] =&gt; Shorts
              [parent] =&gt; 9
          )
  
  )
  </code></pre>
  
  <p>As you can see the child items have the id of the parent items (parent is set to 0), but I'm not sure how to merge it all together to do an array something like this:</p>
  
  <p><strong>parent</strong></p>
  
  <p>-- child</p>
  
  <p>-- child</p>
  
  <p><strong>parent</strong></p>
  
  <p><strong>parent</strong></p>
  
  <p>-- child</p>
  
  <p>-- child</p>
  
  <p>-- child</p>
  
  <p>Any help would be appreciated :)</p>