I have the following MySQL database :
| 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 |
I am trying to return an array as so:
Array
(
[0] => Array
(
[sessionId] => 4
[time] => Array
(
[0] => 2013-03-23 09:00:00
[1] => 2013-03-23 09:15:00
)
)
[1] => Array
(
[sessionId] => 5
[time] => Array
(
[0] => 2013-04-20 01:00:00
[1] => 2013-04-20 15:15:00
[2] => 2013-04-20 19:15:00
)
)
)
So far running this query
$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" =>$row['sessionId'], "time" => array($row['time']));
}
print_r($sessionIds,true);
Gives me the follwing
Array
(
[0] => Array
(
[sessionId] => 4
[time] => Array
(
[0] => 2013-03-23 09:00:00
)
)
[1] => Array
(
[sessionId] => 4
[time] => Array
(
[0] => 2013-03-23 09:15:00
)
)
[2] => Array
(
[sessionId] => 5
[time] => Array
(
[0] => 2013-04-20 01:00:00
)
)
[3] => Array
(
[sessionId] => 5
[time] => Array
(
[0] => 2013-04-20 15:15:00
)
)
[4] => Array
(
[sessionId] => 5
[time] => Array
(
[0] => 2013-04-20 19:15:00
)
)
)
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.
Thanks
GROUP'ed, why do you insist on doing so in PHP? Why not useGROUP_CONCAT,GROUP BY, or any other way of grouping a resultset. Also: Stop using the deprecatedmysql_*extension