Php json数组fomate问题
我想显示二维json数组。 我用php动态编写查询,如下所示Php json数组fomate问题,php,mysql,json,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
$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:00am有两个时间,所以10:00am有子数组,在该数组中有两个数组,如下所示
[
{
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"
}
}
}
]
表示一个数组中的同一时间。
提前感谢分析您想要的输出有点困难。。。如果我理解正确,这可能会帮助您:
$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"
}
]
}
那么你是如何解决这个问题的呢?有什么不适用于该解决方案?显示您的
$select\u query
.select s.*,st.schu\u name from ds\u schedule s left join ds\u schedule\u type st on st.id=s.schtype\u id谢谢@bohrsty我得到了确切的答案它真的帮了我很多忙