简体   繁体   English

在PHP的if语句中使用for循环

[英]Using for loop within if statement in PHP

I am writing a rostering app. 我正在编写排班应用程序。 This statement checks that by booking an extra shift the user doesn't violate a rule whereby they have booked more than 7 night shifts in a row. 该语句检查通过预订额外的班次,用户是否违反了连续预订超过7个夜班的规则。 This code runs fine but I am trying to find a more elegant way to write it, for instance using a for loop within the if statement. 这段代码运行良好,但是我试图找到一种更优雅的编写方式,例如在if语句中使用for循环。 This snippet exists within a bigger while loop. 此代码段存在于较大的while循环中。

if (
    $original_shift->night_shift==true &&
    $p_lookback_night_7===[1,1,1,1,1,1,1] || $p_lookforward_night_7===[1,1,1,1,1,1,1] ||
    ($p_lookback_night_1===[1] && $p_lookforward_night_6===[1,1,1,1,1,1]) ||
    ($p_lookback_night_2===[1,1] && $p_lookforward_night_5===[1,1,1,1,1]) ||
    ($p_lookback_night_3===[1,1,1] && $p_lookforward_night_4===[1,1,1,1]) ||
    ($p_lookback_night_4===[1,1,1,1] && $p_lookforward_night_3===[1,1,1]) ||
    ($p_lookback_night_5===[1,1,1,1,1] && $p_lookforward_night_2===[1,1]) ||
    ($p_lookback_night_6===[1,1,1,1,1,1] && $p_lookforward_night_1===[1])
) {
    return 'You can\'t do more than 7 night shifts in a row'; 
    break;
}

The $p_look variables get populated by a loop looking either back of forward the specified number of days at the end of the variable name and returning an array of true or false for that number of days dependent on whether those are night shifts or not. $p_look变量由一个循环填充,该循环在变量名的末尾向前向前指定的天数返回,并根据该天数是否为夜班,返回该天数的true或false数组。

As an alternative to building several arrays and complex comparisons, this alternative just uses 2 arrays, one with the days prior and one looking forward. 作为构建多个数组和进行复杂比较的一种替代方法,该替代方法仅使用2个数组,一个具有前几天的数组,另一个具有前瞻性。 I'm not 100% sure if this includes the day they are trying to book off, but hopefully the idea is easy enough to adjust to your needs. 我不确定100%是否包括他们要预订的日期,但是希望这个想法很容易就可以适应您的需求。

The basic concept is to look backwards through the $p_lookback_night list and count the 1's, stopping when it reaches a 0. It then does a similar thing through the $p_lookforward_night list. 基本概念是向后看$p_lookback_night列表并计数1,在计数到0时停止。然后通过$p_lookforward_night列表执行类似的$p_lookforward_night The end result is the number of 1's in a row... 最终结果是连续的1个数字...

$p_lookback_night = [0,0,0,0,1,1];
$p_lookforward_night = [1,1,1,1,0,0];
$run = 0;
foreach (array_reverse($p_lookback_night) as $test )  {
    if ( $test == 1 )    {
        $run++;
    }
    else    {
        break;
    }
}

foreach ($p_lookforward_night as $test )  {
    if ( $test == 1 )    {
        $run++;
    }
    else    {
        break;
    }
}

echo $run;

With the test data it gives 6, so you can use this to decide if they are trying to book 7 in a row. 有了测试数据,它得出6,因此您可以使用它来确定他们是否尝试连续预订7。

Assuming all those arrays can only contain 1 in this case you can simply just count the values 假设所有这些数组在这种情况下只能包含1,则只需对值进行计数

&& count($p_lookback_night_7)===7 || ...

Maybe even use the int at the end dynamically but this will be probably more trouble that it is worth. 甚至可以动态地在末尾使用int,但这可能会更值得解决。 Something like 就像是

for($i=1;$i<8;$i++){
  if(count(${"p_lookback_night_".$i}) == $i && count(${"p_lookforward_night_".$i}) == $i ){
  ..wahtever
  }
}

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

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