php,mysql - 将结果合并到多维关联数组中

时间:2013-03-20 14:10:25

标签: php mysql arrays multidimensional-array associative-array

我有以下MySQL数据库:

    | 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 |

我正在尝试返回一个数组:

    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
                    )

            )

    )

到目前为止运行此查询

    $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);

给我以下内容

    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
                        )

                )

        )

所以我想将所有共享同一'sessionId'的'时间'组合在一起。 使用php执行此操作的最佳方法是什么?

由于

4 个答案:

答案 0 :(得分:2)

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

你可以使用mysql的group_concat。这将以逗号分隔字段为您提供结果,您可以使用php进行爆炸以获取数组。

输出:

| 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|

已编辑:

$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);

结果

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
                    )

            )
    )   

答案 1 :(得分:2)

试试:

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'];
}

答案 2 :(得分:0)

从该数组中删除一个级别,并使用sessionID作为键,例如

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

这会给你类似的东西:

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

请注意,您在代码中丢失了一行,因为fetch()调用了循环的OUTSIDE,然后立即使用foreach循环中的fetch调用覆盖$row

答案 3 :(得分:0)

试试这个

<?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
                   )
           )

   )