我正在尝试将值推送到数组中,结果就像
[{"Monday":"11:30"},{"Monday":"12:00"},{"Monday":"23:00"},{"Tuesday":"11:30"},{"Tuesday":"12:00"},{"Tuesday":"23:00"}]
但我希望它成为一个对象
{
"Monday" : [ "11:30", "12:00", "23:00"]
"Tuesday" : ["11:30","12:00","23:00"]
}
这是我的代码片段
$timings = DoctorTimings::where('doctor_id', '=', $input['doctor_id'])->get();
$doctor_timings = [];
foreach($timings as $timing) {
$day_id = $timing->day_id;
$time_slot_id = $timing->time_slot_id;
$day_id = Days::where('id', '=', $day_id)->first()->day;
$time_id = DB::table('time_slot')->where('id', '=', $time_slot_id)->select('time')->first()->time;
array_push($doctor_timings, array($day_id => $time_id));
}
$doctorTimings = json_encode($doctor_timings);
echo $doctorTimings;
侃侃无极