在php中重复数组

时间:2014-02-09 15:59:00

标签: php arrays sorting

我想在数组php中重复元素,每次开始重复时都要考虑选择不同的元素表单数组。 我有这样的代码:

    $numbermonth=$_GET['month'];
    $year=$_GET['year'];
    $day_list = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
    $days_in_month = date("t", mktime(0, 0, 0, $numbermonth, 1, $year));
    $first_day_in_month = date('D', mktime(0,0,0, $numbermonth, 1, $year)); 
      for($i=0; $i<=$days_in_month; $i++){
       //i want echo here  days names, first day name must be $first_day_in_month 
       // and repeat days yet array is not ended
       echo $day_list[$i];

      }

2 个答案:

答案 0 :(得分:2)

看起来你正试图在给定月份中每天循环并输出一周中的某一天。

$year = 2019;
$numbermonth = 2;

$start = new DateTime('midnight');
$start->setDate($year, $numbermonth, 1);    
$period = new DatePeriod($start, new DateInterval('P1D'), $start->format('t') - 1);

foreach ($period as $date) {
    echo $date->format('D') . PHP_EOL;
}

答案 1 :(得分:1)

我想你应该直接循环这些日子并获得名字,但如果你真的想要追求你的想法,你可以做类似的事情

$day_list = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
$end = end($day_list);
reset($day_list);
$days_in_month = date("t", mktime(0, 0, 0, 1, 1, 2014));
for($i = 0; $i <= $days_in_month; $i++)
{
    echo current($day_list).'<br>';

    if (current($day_list) == $end)
        reset($day_list);
    else
        next($day_list);

}