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

时间:2014-03-21 13:46:33

标签: php sql arrays json

我有三个不同的表:

holidays:
id | date | title | description

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

categories:
id | catName

一个事件可以包含一个或多个类别。

我想创建以下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" }
                ]
        }
    ]
}

我提出了一个问题:

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

但我真的不知道如何创建提到的json数组。

2 个答案:

答案 0 :(得分:0)

我没有足够的时间来检查,但其中一个解决方案是:

$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 :(得分:-1)

猜猜它会是这样的:

$Output = Array(
    "Events" => Array (
        Array(
            "id" => $row['event_id'],
            "date" => $row['event_date'],
            "title" => $row['event_title'],
            "description" => $row['description'],
            "categories" => Array (
                Array (
                    'id' => $row['categories'][0]['category_id'],
                    'catName' => $row['categories'][0]['category_name']
                ),
                Array (
                    'id' => $row['categories'][1]['category_id'],
                    'catName' => $row['categories'][1]['category_name']
                )
            )
        )
    )
);

最后

echo json_encode($Output);

产:

{"Events":{"id":null,"date":null,"title":null,"description":null,"categories":[{"id":null,"catName":null},{"id":null,"catName":null}]}}