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 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug