db_query group by to array
You cannot do this using native Drupal 7 functionality. You need to do that grouping in PHP. You should be using a foreach, not a while loop :)


  The most common case is to iterate over the result set with a
  foreach() loop.


Source: https://drupal.org/node/1251174