我从数据库中提取许多这样的行
$rows = [
['id' => 3, 'course_name' => 'GSG2', 'course_day' => 'Monday', 'course_starts_at' => '01:30:00'],
['id' => 2, 'course_name' => 'Look1', 'course_day' => 'Wednesday', 'course_starts_at' => '03:10:00']
]
我需要从中创建一个多维数组,该数组将工作日作为键,将行数组作为值。因为即使当天没有空,我也需要钥匙在场,所以我首先这样做了
$timetable = [
['Monday' => ''],
['Tuesday' => ''],
['Wednesday' => ''],
['Thursday' => ''],
['Friday' => ''],
];
然后我就这样做
foreach ($rows as $row) {
switch ($row['course_day']) {
case "Monday":
$timetable['Monday'] = $row;
break;
case "Tuesday":
$timetable['Tuesday'] = $row;
break;
case "Wednesday":
$timetable['Wednesday'] = $row;
break;
case "Thursday":
$timetable['Thursday'] = $row;
break;
case "Friday":
$timetable['Friday'] = $row;
break;
}
}
但随后生成的 multidim 数组每天只有一个课程(其最后一次迭代)。我尝试使用 += 而不是 = 但那不起作用。请问您有什么建议吗?
交互式爱情
Qyouu