title: php, mysql - Combine result into multidimension associate array
body: <p>I have the following MySQL database :<br></p>
  
  <pre><code>    | id         | sessionId   | time                |
      |------------|-------------|---------------------|
      | 54         |           4 | 2013-03-23 09:00:00 |
      | 55         |           4 | 2013-03-23 09:15:00 |
      | 56         |           5 | 2013-04-20 01:00:00 |
      | 57         |           5 | 2013-04-20 15:15:00 |
      | 58         |           5 | 2013-04-20 19:15:00 |
  </code></pre>
  
  <p>I am trying to return an array as so:</p>
  
  <pre><code>    Array
      (
          [0] =&gt; Array
              (
                  [sessionId] =&gt; 4
                  [time] =&gt; Array
                      (
                          [0] =&gt; 2013-03-23 09:00:00
                          [1] =&gt; 2013-03-23 09:15:00
                      )
  
              )
  
          [1] =&gt; Array
              (
                  [sessionId] =&gt; 5
                  [time] =&gt; Array
                      (
                          [0] =&gt; 2013-04-20 01:00:00
                          [1] =&gt; 2013-04-20 15:15:00
                          [2] =&gt; 2013-04-20 19:15:00
                      )
  
              )
  
      )
  </code></pre>
  
  <p>So far running this query</p>
  
  <pre><code>    $result = mysql_query("SELECT * FROM `Exercise_Lists_New` WHERE `patientId` = 381");
      $row = mysql_fetch_assoc($result);
      while($row = mysql_fetch_array($result)){
          $sessionIds[] = array("sessionId" =&gt;$row['sessionId'], "time" =&gt; array($row['time']));
      }
      print_r($sessionIds,true);
  </code></pre>
  
  <p>Gives me the follwing</p>
  
  <pre><code>    Array
          (
              [0] =&gt; Array
                  (
                      [sessionId] =&gt; 4
                      [time] =&gt; Array
                          (
                              [0] =&gt; 2013-03-23 09:00:00
                          )
  
                  )
  
              [1] =&gt; Array
                  (
                      [sessionId] =&gt; 4
                      [time] =&gt; Array
                          (
                              [0] =&gt; 2013-03-23 09:15:00
                          )
  
                  )
  
              [2] =&gt; Array
                  (
                      [sessionId] =&gt; 5
                      [time] =&gt; Array
                          (
                              [0] =&gt; 2013-04-20 01:00:00
                          )
  
                  )
  
              [3] =&gt; Array
                  (
                      [sessionId] =&gt; 5
                      [time] =&gt; Array
                          (
                              [0] =&gt; 2013-04-20 15:15:00
                          )
  
                  )
  
              [4] =&gt; Array
                  (
                      [sessionId] =&gt; 5
                      [time] =&gt; Array
                          (
                              [0] =&gt; 2013-04-20 19:15:00
                          )
  
                  )
  
          )
  </code></pre>
  
  <p>So i would like to group all the 'time' that share the same 'sessionId' together. 
  What would be the best way to do this using php. </p>
  
  <p>Thanks</p>