php - 按一列值对多个 mysql 结果进行分组

标签 php mysql arrays json

我正在尝试从 mysql 表中获取所有结果并转换为 Json 格式。每行可能包含一个公共(public)列值(movie_name)。我想按该共同字段对结果进行分组。

这是我使用此查询得到的第一个结果

$slots = SELECT movie_name,id as slot_id,screen_id,time,price FROM `slots`;

结果:

{
        "movies": [
            {
                "movie_name": "iceage",
                "slot_id": "142",
                "screen_id": "45",
                "time": "6.00",
                "price": "204"
            },
            {
                "movie_name": "lights out",
                "slot_id": "146",
                "screen_id": "45",
                "time": "11.00",
                "price": "150"
            },
            {
                "movie_name": "lights out",
                "slot_id": "147",
                "screen_id": "45",
                "time": "2.00",
                "price": "103"
            },
            {
                "movie_name": "conjuring",
                "slot_id": "148",
                "screen_id": "45",
                "time": "9.00",
                "price": "500"
            },
            {
                "movie_name": "iceage",
                "slot_id": "151",
                "screen_id": "45",
                "time": "",
                "price": "11"
            },
            {
                "movie_name": "lights out",
                "slot_id": "155",
                "screen_id": "45",
                "time": "11",
                "price": "11"
            }
        ]
}

我想像这样改变上面的结果:-

{
    "movies": [
            {
                "movie_name": "lights out",
                "slots":[ {
                    "slot_id": "146",
                    "screen_id": "45",
                    "time": "11.00",
                    "price": "150"
                }
                {
                    "slot_id": "147",
                    "screen_id": "45",
                    "time": "2.00",
                    "price": "103"
                }
                {
                    "slot_id": "155",
                    "screen_id": "45",
                    "time": "11",
                    "price": "11"
                }
               ]
            },
            {
                "movie_name": "conjuring",
                "slots": {
                    "slot_id": "148",
                    "screen_id": "45",
                    "time": "9.00",
                    "price": "500"
                }
            },
            {
                "movie_name": "iceage",
                "slots":[ {
                    "slot_id": "142",
                    "screen_id": "45",
                    "time": "6.00",
                    "price": "204"
                }
                {
                    "slot_id": "151",
                    "screen_id": "45",
                    "time": "",
                    "price": "11"
                }
               ]
            },
        ]
  }

我应该如何更改查询以获得上述结果。我尝试使用以下 PHP 代码:

                            foreach ($slots as $row) {
                                $movie_name = $row['movie_name'];
                                if (!isset($groups[$movie_name])) {
                                    $groups[$movie_name] = array();
                                }
                            $groups[$movie_name][] = $row;
                            }

但没有得到所需的结果。请帮忙。谢谢..

最佳答案

$json_arr = array("movies"=>array());
foreach ($slots as $slot) {
    $json_arr['movies'][$slot["movie_name"]]['movie_name'] = $slot["movie_name"];
    $json_arr['movies'][$slot["movie_name"]]['slots'][] = array (
        "slot_id" => $slot["slot_id"],
        "screen_id" => $slot["screen_id"],
        "time" => $slot["time"],
        "price" => $slot["price"]
    );
}

关于php - 按一列值对多个 mysql 结果进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38553204/

相关文章:

php - 从 ajax 中检索 php 数组

php - 检查 MySQL 数据库中的唯一标识时出现问题

javascript - 从仅包含发送的查询的数组中排除结果。 MongoDB

php - Magento 动态更改运费 SQLSTATE[23000]

Php 信息显示不正确的默认时区(UTC)

PHPMailer - 某些 SMTP 消息不发送

mysql - Amazon EC2 实例每天变慢

php - 如何在php中制作循环

python - 比较数组的准确性

Python 和一维谱 - 如何访问拟合文件中的数据