2

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

1
  • 1
    If you want the data you get from the query GROUP'ed, why do you insist on doing so in PHP? Why not use GROUP_CONCAT, GROUP BY, or any other way of grouping a resultset. Also: Stop using the deprecated mysql_* extension Commented Mar 20, 2013 at 14:12

4 Answers 4

2

Just try with:

while($row = mysql_fetch_array($result)){
    if ( !isset($sessionIds[$row['sessionId']]) ) {
        $sessionIds[$row['sessionId']] = array(
            'sessionId' => $row['sessionId'],
            'time'      => array()
        );
    }
    $sessionIds[$row['sessionId']]['time'][] = $row['time'];
}
Sign up to request clarification or add additional context in comments.

Comments

2
SELECT
    sessionId,
    GROUP_CONCAT(time) AS `Times`
FROM mytable
GROUP BY    sessionId

You can use group_concat of mysql for this. This will give you results as comma seperated field which you can explode with php to get an array.

Output :

| sessionId   | time                                                           |
|-------------|--------------------------------------------------------------- |
|           4 | 2013-03-23 09:00:00 , 2013-03-23 09:15:00                      |
|           5 | 2013-04-20 01:00:00 , 2013-04-20 15:15:00 , 2013-04-20 19:15:00|

EDITED :

$query  =   "   SELECT
                    sessionId,
                    GROUP_CONCAT(time) AS `Times`
                FROM mytable
                GROUP BY    sessionId";

$result = mysqli_query($query);
$row = mysqli_fetch_assoc($result);
$result_array   =   array();
$i=0;
while($row = mysql_fetch_array($result)){
    $result_array[$i]['sessionId']  = $row['sessionId'];
    $result_array[$i]['time']       = explode(',',$row['Times']);
    $i++;
}
print_r($result_array,true);

Result

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
                    )

            )
    )   

Comments

0

Strip one level off that array, and use sessionID as the key, e.g.

$sessionIDs = array();
while($row = mysql_fetch_array($result)){
    $sessionIds[$row['sessionID']][] = $row['time'];
}

This'll give you something like:

$arr = array(
     'id' => array(time1, time2, time3, ...),
     'id' => arraY(...)
     etc..
);

Note that you're losing one row in your code, as you do a fetch() call OUTSIDE of your loop, then immediately overwrite that $row with the fetch call in the foreach loop.

Comments

0

Try with this

<?php

$result = mysql_query("SELECT * FROM `Exercise_Lists_New` WHERE `patientId` = 381");
$row = mysql_fetch_assoc($result);
while($row = mysql_fetch_array($result)){    
    $sessionIds[$row['sessionId']]['sessionId'] = $row['sessionId'];
    $sessionIds[$row['sessionId']]['time'][] = $row['time'];
}

print_r($sessionIds):

Array
   (
       [4] => Array
           (
               [sessionId] => 4
               [time] => Array
                   (
                       [0] => 2013-03-23 09:00:00
                       [1] => 2013-03-23 09:15:00
                   )

           )

       [5] => 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
                   )
           )

   )

1 Comment

this gives me multiple sessionIDs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.