【问题标题】:Nested foreach loop in a While loop can make the condition for the while loop go over?While循环中嵌套的foreach循环可以使while循环的条件结束吗?
【发布时间】:2015-09-08 04:45:56
【问题描述】:

这里是我正在尝试创建的一些背景。

我正在创建一个名为getNextBilling($dateStart,$dateCount = 20)的函数

您给它一个周期长度,即您希望某人被计费的天数

$test->period = '2,5,15';

它需要一个开始日期 我已经在测试页面上分配了

$test->getNextBilling('2015-06-12 00:00:00',2);

该功能应该做的是确保输入的日期小于当前日期以跳过本月计费,这非常有效。

什么不起作用是我的 while 循环中有一个 foreach 循环来查看输入的所有天数,我找不到让代码在实际只显示天数的地方工作的方法你想要的。

上面的例子显示我想显示 2 天的输出是

Array
(
    [1] => 2015-06-15
    [2] => 2015-07-02
    [3] => 2015-07-05
    [4] => 2015-07-15
)

这是函数的代码

// SET START DATE
$startDate = new DateTime($dateStart);
// LOOP
$i = 1;
while($i <= $dateCount){
    // LOOP THROUGH INPUTED DAYS
    foreach($days as $day){
        // CLEAN DAY
        $day = formatNumbersOnly($day);                     
        // SET LAST DAY
        $lastDay = new DateTime($startDate->format('Y-m-d'));
        $lastDay->modify('last day of this month');
        // CHECK FOR PASSED DAY
        if($day > $startDate->format('j')){
            // CHECK FOR 28-29-30-31
            if($day > $lastDay->format('j')){
                // SET NEW DATE
                $startDate->setDate(
                    $startDate->format('Y'),
                    $startDate->format('m'),
                    $lastDay->format('j')
                );
            } else {
                // SET NEW DATE
                $startDate->setDate(
                    $startDate->format('Y'),
                    $startDate->format('m'),
                    $day
                );
            }
            // SET ARRAY
            $nextBilling[$i] = $startDate->format('Y-m-d');

        } else {
            // SKIP
            $i--;
        }
        // INCREASE COUNT
        $i++;
    }               
    // NEXT MONTH
    $startDate->modify('1 month');
    $startDate->setDate($startDate->format('Y'),$startDate->format('m'),1);
}

现在我明白问题出在哪里了,这是因为 foreach 循环内的计数增加了,但是当我把它放在外面时,它只会输出最后一天,就像这样:

Array ( [-1] => 2015-06-15 [0] => 2015-07-15 [1] => 2015-08-15 [2] => 2015-09-15 )

如果有人有任何批评或提示,将不胜感激。我将继续尝试修复它。

编辑:我在foreach 的开头添加了一个if 语句来检查$i 是否大于$dateCount 它会跳出循环。感谢 Twisty 的时间和努力,我很感激。

【问题讨论】:

  • 在您的示例中,我看不到您的 foreach 循环在哪里关闭。
  • foreach 在// INCREASE COUNT $i++; 之后关闭
  • $days 定义在哪里?
  • 对不起,我忘了补充,// EXPLODE CONTENTS TO FIND DAYS $days = explode(',', $this-&gt;period);
  • $days 中预期的值是多少?

标签: php loops foreach while-loop


【解决方案1】:

很高兴您找到了答案。这是你找到它时我正在做的事情。

// SET START DATE
$startDate = new DateTime($dateStart);
$nextBill = new DateTime();

for($i=1;$i<$countDays;$i++){
    switch(true){
        case ($startDate->format('j') > $days[2]):
            // go to next month
            $nextBill->setDate(
                $startDate->format('Y'),
                $startDate->format('m')+1,
                $days[0];
            );
            break;
        case ($startDate->format('j') > $days[1]):
            // Is start date greater than 2nd billing (5)
            $nextBill->setDate(
                $startDate->format('Y'),
                $startDate->format('m'),
                $days[2];
            );
            break;
        case ($startDate->format('j') > $days[0]):
            // is starteDate greater than 1st billing (2)
            $nextBill->setDate(
                $startDate->format('Y'),
                $startDate->format('m'),
                $days[1];
            );
            break;
        default:
            $nextBill->setDate(
                $startDate->format('Y'),
                $startDate->format('m'),
                $days[0];
            );
    }
    $nextBilling[$i] = $nextBill->format('Y-m-d');
    $startDate = $nextBill->setDate(
        $nextBill->format('Y'),
        $nextBill->format('m'),
        $nextBill->format('j')+1
    );
}

这将确保如果$startDate 是本月的 1 号,它会显示 2 号,然后是 5 号。 ID $startDate 是第 3 个,它将显示第 5 个和第 15 个。如果$startDate 是 20 号,则显示下个月的 2 号和 5 号。

【讨论】:

  • 这看起来不错,但是您是否必须更改代码以允许输入更多天数?在声明期间用户可以,如果他们想去period(1,2,3,4,5,6,7,8,9,10)
  • 是的,你会的。您可能希望基于 $days 的count() 进行循环,然后递减。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 2013-10-01
  • 1970-01-01
  • 2014-12-05
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
相关资源
最近更新 更多