title: PHP tree structure for categories and sub categories without looping a query
body: <p>I'm trying to create a list of categories with any number of sub categories, where sub categories can also has their own sub categories.</p>
  
  <p>I have selected all categories from the Mysql db, the cats are in a standard associate array list, each category has an id, name, parentid where the parentid is 0 if it's top level.</p>
  
  <p>I basically want to be able to take the single level array of cats and turn it into a multidimensional array structure where each category can have an element which will contain an array of subcats.</p>
  
  <p>Now, I can easily achieve this by looping a query for each category but this is far from ideal, I'm trying to do it without any extra hits on the db.</p>
  
  <p>I understand I need a recursive function for this. Can anyone point me in the right direction for this tree style structure?</p>
  
  <p>Cheers</p>