正如您在下面的代码中看到的,2020-01-13 上有两个约会,但它们是作为单独的数组生成的。既然它们在同一天,我怎么能把它们放在同一个阵列上?我正在使用 mysqli 从数据库中获取所有信息,但为了这段代码,我只打印必要的信息。它有效,但我需要知道如何在同一数组中收集具有相同日期的约会。
php
//declare variable
$result = array();
//iterate
while($schedule = $result_data->fetch_assoc()){
//start time conversion into american time
// 24-hour time to 12-hour time
$start_time =date("g:i a", strtotime($schedule['start_time']));
//end time conversion into american time
$end_time =date("g:i a", strtotime($schedule['end_time']));
$my_schedule_start .= $start_time;
$my_schedule_end .= $end_time;
$schedule_date= $schedule['cleaning_date'];
$result[] = array(
"$schedule_date" => [
"number"=>'1',
"badgeClass"=>'',
"url"=>'url',
"dayEvents"=>[
"title"=>$schedule['first_name'].' '.$schedule['last_name'],
"status"=>$schedule['cleaning_status'],
"time"=>$start_time .' - '.$end_time
]
]
);
}
//send response back to jquery
header('Content-Type: application/json');
echo json_encode($result);
这个 php 打印这个
[{"2020-01-15":{
"number":"1",
"badgeClass":"",
"url":"url",
"dayEvents":{
"title":"Jen Doe",
"status":"Booked",
"time":"11:00 am - 12:00 pm"}
}
},
{"2020-01-13":{
"number":"1",
"badgeClass":"",
"url":"url",
"dayEvents":{
"title":"John Doe",
"status":"Booked",
"time":"2:00 pm - 5:00 pm"}
}
},
{"2020-01-13":{
"number":"1",
"badgeClass":"",
"url":"url",
"dayEvents":{
"title":"Alfred Doe",
"status":"Booked",
"time":"11:00 am - 12:00 pm"}
}
}
]
但我想要的结果是这样
[{
"2020-01-15": {
"number": "1",
"badgeClass": "",
"url": "url",
"dayEvents": {
"title": "Jen Doe",
"status": "Booked",
"time": "11:00 am - 12:00 pm"
}
}
},
{
"2020-01-13": {
"number": "2",
"badgeClass": "",
"url": "url",
"dayEvents": [{
"title": "John Doe",
"status": "Booked",
"time": "2:00 pm - 5:00 pm"
},
{
"title": "Alfred Doe",
"status": "Booked",
"time": "11:00 am - 12:00 pm"
}
]
}
}
]