我有一个月份的数组。我想通过每个月作为键来迭代获取每个月的值,但是...
我想从当前月份开始,运行foreach循环,然后回到下一年,在第12个月停止。
我尝试创建一个基于当前月份的单独月份数组,但这似乎有点不稳定。
我想从当前月份开始,运行foreach循环,然后回到下一年,在第12个月停止。
我尝试创建一个基于当前月份的单独月份数组,但这似乎有点不稳定。
do/while
循环,例如:$months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
$i = $current_month = 5;
do {
echo $months[$i] . "\n";
$i = ($i + 1) % 12;
} while ($i != $current_month);
输出:
Jun
Jul
Aug
Sep
Oct
Nov
Dec
Jan
Feb
Mar
Apr
May
$year = 2018;
$i = $current_month = 5;
do {
echo $months[$i] . " $year\n";
$i = ($i + 1) % 12;
if ($i == 0) $year++;
} while ($i != $current_month);
输出:
Jun 2018
...
Dec 2018
Jan 2019
...
May 2019
continue
语句!$current_month = 6; # just assuming, you can change as per your requirement.
foreach ($month_array as $k => $v) {
if ($k < 5) continue;
// your code here to go after your current month to end of the year's month
}
for
语句呢?$months = [
'January',
'February',
'...',
];
$currentMonth = 5; // 0 for January, 11 for December
for($i = 0; $i < 12; $i++) {
$index = ($currentMonth + $i) % 12;
echo $months[$index] . PHP_EOL;
}
将打印
June
July
August
September
...