简体   繁体   English

跳过do-while循环中的值

[英]Skip a value in do-while loop

I've got a script that sets up a array of dates which form a dropdown. 我有一个脚本,可以设置一系列日期,这些日期构成一个下拉列表。 The array is set up by some variables about the current date and an fixed offset for the last date (two weeks). 该数组由一些有关当前日期的变量设置,并为最后一个日期(两周)提供固定的偏移量。 Stripped down this is what it looks like: 剥离下来是这样的:

public function getDatesOptionArray()
{
    $datesArray = array();
    $displayDate = Mage::getModel('core/locale')->storeDate();
    $displayDate->add($this->_startDaysOffset, Zend_Date::DAY);
    $dayOffset = $this->_startDaysOffset;

    do
    {
        $dayofweek = date('w', strtotime($displayDate->toString(Varien_Date::DATE_INTERNAL_FORMAT)));

        $datesArray[$displayDate->toString(Varien_Date::DATE_INTERNAL_FORMAT)] = Mage::helper('core')->formatDate($displayDate, Mage_Core_Model_Locale::FORMAT_TYPE_FULL);
        $displayDate->add('1', Zend_Date::DAY);
        $dayOffset++;

    } while ($dayOffset <= $this->_endDaysOffset);

    return $datesArray;
}

The thing is I want to leave out all the 'Sunday' options, and I've got the $dayofweek variable for each, where sunday is 0. I've tried to wrap the whole thing inside the do function in an if-statement ( if $dayofweek !== 0 ), set an if ($dayofweek == 0) { continue;} and every other trick I could think of, but I get only either of these results 问题是我想忽略所有的“星期日”选项,并且每个变量都有$dayofweek变量,其中星期日为0。我试图将整个函数包装在if语句中的do函数中( if $dayofweek !== 0 ),则设置一个if ($dayofweek == 0) { continue;}以及我可能想到的所有其他技巧,但我只能得到这些结果之一

  • 1: Only the first date is shown 1:仅显示第一个日期
  • 2: All dates until the first sunday are shown, none after that 2:显示直到第一个星期日的所有日期,之后没有任何日期
  • 3: All dates are shown 3:显示所有日期

I think I might be missing the point on the do-while loop; 我想我可能会错过do-while循环的意义; how do I exclude if $dayofweek == 0 ? if $dayofweek == 0如何排除?

For me is something like this, i use while because Do...While(...) the first time it will not check your condition you'll enter your loop at last 1 time, and when you use while(...){} every time your program will check your condition 对我来说是这样的,我使用while是因为Do ... While(...)第一次不会检查您的状况,您将在最后一次进入循环,而当您使用while(... ){},每次程序检查您的状况时

public function getDatesOptionArray()
{
  $datesArray = array();
  $displayDate = Mage::getModel('core/locale')->storeDate();
  $displayDate->add($this->_startDaysOffset, Zend_Date::DAY);
  $dayOffset = $this->_startDaysOffset;


  while ($dayOffset <= $this->_endDaysOffset)
  {
   $dayofweek = date('w', strtotime($displayDate->toString(Varien_Date::DATE_INTERNAL_FORMAT)));
   if ($dayofweek != 0) {
      $datesArray[$displayDate->toString(Varien_Date::DATE_INTERNAL_FORMAT)] = Mage::helper('core')->formatDate($displayDate, Mage_Core_Model_Locale::FORMAT_TYPE_FULL);
    }
    $displayDate->add('1', Zend_Date::DAY);

    $dayOffset++;
  }
  return $datesArray;
}

Let say the array is 假设数组是

$date = array(
    2017-10-12,
    2017-10-13,
    2017-10-14,
    2017-10-15,
    2017-10-16,
    2017-10-17,
    2017-10-18,
    2017-10-19,
    2017-10-20,
    2017-10-21,
    2017-10-22,
    2017-10-23,
    2017-10-24,
    2017-10-25
);

Now do this for remove sunday's dates 现在执行此操作以删除星期日的日期

$i=0;
do{
    if(date('w',strtotime($date[$i]))>0) $dateArray[] = $date[$i];
    $i++;
} while ($i<count($date));
echo "<pre>";print_r($dateArray);
return $datesArray;

$dateArray will give you dates without sundays. $ dateArray将给您没有星期天的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM