我使用以下php查询从mySQL获取此JSON结果:
/* grab the posts from the db */
$query = "SELECT * FROM ko_timetable";
$result = mysql_query($query,$link) or die('Errant query: '.$query);
/* create one master array of the records */
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
$posts[] = array('post'=>$post);
}
}
JSON结果:
post = {
"CLASS_LEVEL" = "Intro/General";
"CLASS_TYPE" = "Muay Thai";
"DAY_OF_WEEK" = Sunday;
ID = 19;
"ORDER_BY" = 5;
TIME = "1:00pm - 2:30pm";
};
}
{
post = {
"CLASS_LEVEL" = "General/Intermediate/Advanced";
"CLASS_TYPE" = "Muay Thai Spar - Competitive";
"DAY_OF_WEEK" = Sunday;
ID = 27;
"ORDER_BY" = 5;
TIME = "6:00pm - 9:00pm";
};
},
{
post = {
"CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
"CLASS_TYPE" = "Fighters Training";
"DAY_OF_WEEK" = Monday;
ID = 1;
"ORDER_BY" = 1;
TIME = "9:30am - 11:00pm";
};
但是如何更改此查询以通过“DAY_OF_WEEK”获取此结果组。请参阅下面的示例,感谢您的帮助:
{
Sunday = {
"CLASS_LEVEL" = "Intro/General";
"CLASS_TYPE" = "Muay Thai";
"DAY_OF_WEEK" = Sunday;
ID = 19;
"ORDER_BY" = 5;
TIME = "1:00pm - 2:30pm";
};
{
"CLASS_LEVEL" = "General/Intermediate/Advanced";
"CLASS_TYPE" = "Muay Thai Spar - Competitive";
"DAY_OF_WEEK" = Sunday;
ID = 27;
"ORDER_BY" = 5;
TIME = "6:00pm - 9:00pm";
};
},
{
Monday = {
"CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
"CLASS_TYPE" = "Fighters Training";
"DAY_OF_WEEK" = Monday;
ID = 1;
"ORDER_BY" = 1;
TIME = "9:30am - 11:00pm";
};
由于
答案 0 :(得分:2)
你可以像这样使用DAY_OF_WEEK作为数组索引,
while($post = mysql_fetch_assoc($result)) {
$posts[$posts["DAY_OF_WEEK"]] = array('post'=>$post);
}
但是请记住,在示例中,您已经显示了数组的索引是日期的名称,那么上周日的结果将被这周的周日替换,依此类推。 您还可以使用count变量来避免替换具有重复名称的键,如下所示。
$count = 1;
while($post = mysql_fetch_assoc($result)) {
$posts[$posts["DAY_OF_WEEK"].$count] = array('post'=>$post);
$count++;
}
答案 1 :(得分:1)
改变这个:
while($post = mysql_fetch_assoc($result)) {
$posts[ $post['DAY_OF_WEEK'] ] = $post;
}