douwei1408 2014-03-21 13:46
浏览 122
已采纳

通过php从MySql创建二维JSON数组

I have three different tables:

holidays:
id | date | title | description

cat_event:
id | eventId (fk: holidays.id) | catId (fk: categories.id)

categories:
id | catName

One event can contain one or more categories.

I'd like to create the following JSON:

{
"Events":
    [
        {
            "id": "12",
            "date": "2014.03.21",
            "title": "National Tiger Day",
            "description": "Some description text",
            "categories":
                [
                    { "id": "1", "catName": "Animal" },
                    { "id": "2", "catName": "Global" }
                ]
        },
        {
            "id": "13",
            "date": "2014.03.22",
            "title": "World Chocolate Day",
            "description": "Some description text",
            "categories":
                [
                    { "id": "3", "catName": "Food" },
                    { "id": "2", "catName": "Global" }
                ]
        }
    ]
}

I made a query:

SELECT holidays.*, categories.* FROM holidays JOIN event_cat ON event_cat.eventId = holidays.id JOIN categories ON categories.id = event_cat.catId WHERE holidays.id = 1

But I really don't have a clue how I can create the mentioned json array.

  • 写回答

2条回答 默认 最新

  • doujian0265 2014-03-21 14:26
    关注

    I have not enough time to check but one of solutions is:

    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    // checking connection
    if (mysqli_connect_errno()) {
        exit();
    }
    if ($result = $mysqli->query("
    SELECT holidays.*, categories.id as cat_id,categories.catName as catName FROM holidays
        JOIN event_cat ON event_cat.eventId = holidays.id
        JOIN categories ON categories.id = event_cat.catId
        WHERE holidays.id = 1")) {
        while ($row = $result->fetch_row()) {
            if(isset($respond['Events'][$row['id']]))
            {
                $response['Events'][$row['id']]['categories'][] = array('id'=>$row['cat_id'],'catName'=>$row['catName']);
            }else
            {
                $response['Events'][$row['id']] = array(
                    'id'=>$row['id'],
                    'date'=>$row['date'],
                    'title'=>$row['title'],
                    'description'=>$row['description'],
                    'categories'=>array(array(
                        'id'=>$row['cat_id'],
                        'catName'=>$row['catName']))
    
                );
            }
        }
        $result->close();
    }
    $mysqli->close();
    echo json_encode($response);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么nacos注册失败了呀?
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 游戏盾如何溯源服务器真实ip?
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥15 抖音看过的视频,缓存在哪个文件