json数组格式问题

时间:2015-01-25 10:52:04

标签: php mysql json

我想显示二维json数组。 我在php中动态编写查询,如下所示。

$result = mysql_query($select_query);
$json_response = array();
$row_array=array();
while ($row = mysql_fetch_array($result)) {
    $row_array[$row['sch_from_time']]['sch_id'] = $row['id'];
    $row_array[$row['sch_from_time']]['sch_title'] = $row['sch_title'];
    $row_array[$row['sch_from_time']]['sch_date'] = $row['sch_date'];
    $row_array[$row['sch_from_time']]['sch_from_time'] = $row['sch_from_time'];
    $row_array[$row['sch_from_time']]['sch_to_time'] = $row['sch_to_time'];
    $row_array[$row['sch_from_time']]['sch_location'] = $row['sch_location'];
    $row_array[$row['sch_from_time']]['sch_latitude'] = $row['sch_latitude'];
    $row_array[$row['sch_from_time']]['sch_longitude'] = $row['sch_longitude'];
    $row_array[$row['sch_from_time']]['sch_type'] = $row['sch_name'];
    $row_array[$row['sch_from_time']]['sch_des'] =  str_replace("\"","'", $row['sch_des']);
    array_push($json_response, $row_array);
    unset($row_array);
}
print_r(json_encode($json_response));

我的输出如下:

  [
{
10:00 AM: {
sch_id: "6",
sch_title: "sample3",
sch_date: "2015-01-25",
sch_from_time: "10:00 AM",
sch_to_time: "11:00 AM",
sch_location: "vizag",
sch_latitude: "10",
sch_longitude: "20",
sch_type: "hi",
sch_des: "dfas asdf dfdasf ddsfasdf asf asdf"
}
},
{
10:00 AM: {
sch_id: "4",
sch_title: "sample1",
sch_date: "2015-01-25",
sch_from_time: "10:00 AM",
sch_to_time: "11:00 AM",
sch_location: "vizag",
sch_latitude: "10",
sch_longitude: "20",
sch_type: "hi",
sch_des: "dfas asdf dfdasf ddsfasdf asf asdf"
}
},
{
02:10 AM: {
sch_id: "1",
sch_title: "Test",
sch_date: "2015-01-05",
sch_from_time: "02:10 Am",
sch_to_time: "12:05 Am",
sch_location: "Vizag",
sch_latitude: "45",
sch_longitude: "45",
sch_type: "hi",
sch_des: "Huiiiiiiiiiiiiiiiiiiiiiii"
}
}
]

但我需要显示所有相同时间显示单键值表示10:00 AM有两次所以10:00 AM在该阵列中有子阵列有两个数组如下所示

[
{
10:00 AM: {[{
sch_id: "6",
sch_title: "sample3",
sch_date: "2015-01-25",
sch_from_time: "10:00 AM",
sch_to_time: "11:00 AM",
sch_location: "vizag",
sch_latitude: "10",
sch_longitude: "20",
sch_type: "hi",
sch_des: "dfas asdf dfdasf ddsfasdf asf asdf"
},
 {
sch_id: "4",
sch_title: "sample1",
sch_date: "2015-01-25",
sch_from_time: "10:00 AM",
sch_to_time: "11:00 AM",
sch_location: "vizag",
sch_latitude: "10",
sch_longitude: "20",
sch_type: "hi",
sch_des: "dfas asdf dfdasf ddsfasdf asf asdf"
}]
},
{
02:10 AM: {
sch_id: "1",
sch_title: "Test",
sch_date: "2015-01-05",
sch_from_time: "02:10 Am",
sch_to_time: "12:05 Am",
sch_location: "Vizag",
sch_latitude: "45",
sch_longitude: "45",
sch_type: "hi",
sch_des: "Huiiiiiiiiiiiiiiiiiiiiiii"
}
}
}
]

表示同一个数组中的相同时间。 提前谢谢

1 个答案:

答案 0 :(得分:0)

分析你想要的输出有点难......如果我理解的话,这可能对你有所帮助:

$json_response = array();
while ($row = mysql_fetch_array($result)) {
    $json_response[$row['sch_from_time']][] = array(
        'sch_id' => $row['id'],
        'sch_title' => $row['sch_title'],
        'sch_date' => $row['sch_date'],
        'sch_from_time' => $row['sch_from_time'],
        'sch_to_time' => $row['sch_to_time']
        'sch_location' => $row['sch_location'],
        'sch_latitude' => $row['sch_latitude'],
        'sch_longitude' => $row['sch_longitude'],
        'sch_type' => $row['sch_name'],
        'sch_des' => str_replace("\"","'", $row['sch_des']),
    );
}
echo json_encode($json_response);

生成以下输出:

{"10:00 AM":
    [
        {"sch_id": "6",
        "sch_title": "sample3",
        "sch_date": "2015-01-25",
        "sch_from_time": "10:00 AM",
        "sch_to_time": "11:00 AM",
        "sch_location": "vizag",
        "sch_latitude": "10",
        "sch_longitude": "20",
        "sch_type": "hi",
        "sch_des": "dfas asdf dfdasf ddsfasdf asf asdf"
        },
        {"sch_id": "4",
        "sch_title": "sample1",
        "sch_date": "2015-01-25",
        "sch_from_time": "10:00 AM",
        "sch_to_time": "11:00 AM",
        "sch_location": "vizag",
        "sch_latitude": "10",
        "sch_longitude": "20",
        "sch_type": "hi",
        "sch_des": "dfas asdf dfdasf ddsfasdf asf asdf"
        }
    ],
"02:10 AM": 
    [
        {"sch_id": "1",
        "sch_title": "Test",
        "sch_date": "2015-01-05",
        "sch_from_time": "02:10 Am",
        "sch_to_time": "12:05 Am",
        "sch_location": "Vizag",
        "sch_latitude": "45",
        "sch_longitude": "45",
        "sch_type": "hi",
        "sch_des": "Huiiiiiiiiiiiiiiiiiiiiiii"
        }
    ]
}