gpt4 book ai didi

php - 按年按月的结果 php array foreach

转载 作者:行者123 更新时间:2023-11-29 12:00:02 25 4
gpt4 key购买 nike

我陷入了 php foreach 循环。我想做的是按年份获取结果,然后按当年的每个月获取结果。

 $sql = "SELECT DATE_FORMAT(datestart, '%M') as month,
MONTH(datestart) AS m,
YEAR(datestart) AS year,
holiday.Id,
employeeId,
datestart,
dateEnd,
DATE_FORMAT(holiday.dateEnd,'%D %M, %Y') AS end,
DATE_FORMAT(holiday.datestart,'%D %M, %Y') AS startit,
DATE_FORMAT(holiday.datestart,'%d-%m-%Y') AS st,
DATE_FORMAT(holiday.datestart,'%d-%m-%Y') AS en,
CONCAT(employees.empFirst,' ',employees.empLast) AS employee,
DATE_FORMAT(holiday.datestart,'%d-%m-%Y %H:%i') AS start
From holiday
LEFT JOIN employees ON holiday.employeeId = employees.empId
ORDER BY year asc, m, datestart
";

$result = $mysqli->query($sql);
$res = $mysqli->query($sql);
$months = array();
while ($row = mysqli_fetch_assoc($result)) {
$months[$row['month']][] = $row;
}

foreach($months as $months => $month) {
echo '<b>Month &nbsp;'.$months. '</b><br>';
foreach($month as $item){
echo $item['employee']. '&nbsp;-&nbsp;'
.$item['startit'].'&nbsp;-&nbsp;'.$item['end'].'<br/>';
}
}

我得到的结果是月份然后是项目,例如。

  • 一月商品详情
  • 三月商品详情
  • 十月商品详情

我追求的是:

  • 2015
  • 一月
  • 商品详情
  • 三月
  • 商品详情
  • 十月
  • 商品详情
  • 2016
  • 一月
  • 列出项目

希望有人能帮忙。谢谢

最佳答案

希望这部分代码对您有所帮助。

# some code

$year = $row['year']; # Important part.
$month = $row['month'];
$something[$year][$month][] = $row

# some code

foreach($something as $year => $months) { # Important part.
# some code
foreach($months as $month => $items) {
# some code
foreach($items as $item) {

# some code

关于php - 按年按月的结果 php array foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32543673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com