【发布时间】:2015-12-09 13:03:40
【问题描述】:
我有一个在 foreach 循环中输出假期的 mysql 查询。例如。 2015 年 1 月 15 日 Steven 假期、3 月 12 日 jim 假期 2 月 20 日 jon 假期 2016 年 1 月 10 日假期等。我如何列出一年中的所有月份而不仅仅是假期的月份?
$monthx = array('January'=>null, 'Febuary'=>null, 'March'=>null, 'April'=>null, 'May'=>null,
'June'=>null, 'July'=>null, 'August'=>null, 'September'=>null, 'October'=>null, 'November'=>null, 'December'=>null);
while ($row = mysqli_fetch_assoc($result)) {
$year = $row['year'];
$month = $row['month'];
$years[$year][$month][] = $row;
//print_r($something);
}
foreach($years as $year => $months) {
echo '<b>Year '.$year. '</b><br>';
foreach($months as $month => $items) {
echo '<b>Month '.$month. '</b><br>';
foreach($items as $item) {
echo $item['employee']. ' - ' .$item['startit'].' - '.$item['end'].'<br/>';
}}}
我的想法是创建一个包含所有月份的数组,然后将该数组与包含来自 mysql 的所有数据的数组合并。我试过这个没有成功。任何帮助都会很棒。
【问题讨论】:
-
您的代码对我来说看起来不错。运行时发生了什么? “没有成功”不是对问题的描述。请阅读minimal reproducible example。
-
@Anders 不确定如何将 $monthx 与 $Month =row['month' 合并以输出一月二月三月等。代码仅在 MySQL 中运行良好,但不确定如何实现 array_merge 或 array_merge_recursive 我有重复的键,例如。一月,三月在mysql查询中。因为我是新手,甚至不确定这是解决这个问题的正确方法。