title: Parse SELECT clause of SQL queries into a PHP array
body: <p>This is more for analysing a query in PHP <strong>before</strong> it's sent to the server. Very complicated why I'm doing this, so I'd rather not go into the reason for this.</p>
  <p>In PHP, I need to store the field selections into a PHP array. So take this query for example:</p>
  <pre><code>SELECT user_id,username,DATE(join_datetime) as join_date, (SELECT COUNT(1) FROM foobar WHERE foonum IN (5,4,6) and user_id = users.user_id) as myfoo_count 
  FROM users 
  WHERE user_id = 123
  </code></pre>
  <p>In this case I need to store <code>user_id,username,DATE(join_datetime) as join_date, (SELECT COUNT(1) FROM foobar WHERE foonum IN (5,4,6) and user_id = users.user_id) as myfoo_count</code> into an array exploded by a comma (,). So I would get:</p>
  <pre><code>array (
    [1] =&gt; 'user_id',
    [2] =&gt; 'username',
    [3] =&gt; 'DATE(join_datetime) as join_date',
    [4] =&gt; '(SELECT COUNT(1) FROM foobar WHERE foonum IN (5,4,6) and user_id = users.user_id) as myfoo_count'
  )
  </code></pre>
  <p>I have gotten as far as extracting the fields part of the query, but I'm stuck on trying to explode the fields by comma. The main problem being with subqueries which might have commas in them too (see example).</p>