PHP for 循环有 2 个变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1172553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:20:05  来源:igfitidea点击:

Php for loop with 2 variables?

phpfor-loopinfinite-loop

提问by jingleboy99

is it possible to do this? (here is my code)

是否有可能做到这一点?(这是我的代码)

for ($i = 0 ; $i <= 10 ; $i++){
  for ($j = 10 ; $j >= 0 ; $j--){
     echo "Var " . $i . " is " . $k . "<br>";
  }
}

I want something like this:

我想要这样的东西:

var 0 is 10

变量 0 是 10

var 1 is 9

变量 1 是 9

var 2 is 8 ...

变量 2 是 8 ...

But my code is wrong, it gives a huge list. Php guru, help me !!

但是我的代码是错误的,它给出了一个巨大的列表。Php大师,帮帮我!!

回答by Gumbo

Try this:

尝试这个:

for ($i=0, $k=10; $i<=10 ; $i++, $k--) {
    echo "Var " . $i . " is " . $k . "<br>";
}

The two variables $iand $kare initialized with 0and 10respectively. At the end of each each loop $iwill be incremented by one ($i++) and $kdecremented by one ($k--). So $iwill have the values 0, 1, …, 10 and $kthe values 10, 9, …, 0.

两个变量$iand$k分别用0和初始化10。在每个循环结束时,$i将增加一 ( $i++) 并$k减少一 ( $k--)。所以$i会有值 0, 1, ..., 10 和$k值 10, 9, ..., 0。

回答by Mihail Minkov

You could also add a condition for the second variable

您还可以为第二个变量添加条件

for ($i=0, $k=10; $i<=10, $k>=0 ; $i++, $k--) {
    echo "Var " . $i . " is " . $k . "<br>";
}

回答by AlbertoPL

You shouldn't be using two for-loops for what you'd like to achieve as you're looping 121 times total (11x11). What you really want is just to have a counter declared outside of the loop that tracks j, and then decrement j inside the loop.

您不应该使用两个 for 循环来实现您想要实现的目标,因为您总共循环了 121 次 (11x11)。您真正想要的只是在跟踪 j 的循环外声明一个计数器,然后在循环内递减 j。

Edit: Thanks Gumbo for catching the inclusion for me.

编辑:感谢 Gumbo 为我捕获包含内容。

回答by bluebrother

If, as your code looks like, you have two values running the opposite direction you could simply substract:

如果,如您的代码所示,您有两个方向相反的值,您可以简单地减去:

echo "Var " . $i . " is " . 10 - $i . "<br>";

But I guess that's not really what you want? Also, be careful with the suggested comma operator. While it is a nice thing it can cause naughty side effects in other languages like C and C++as PHP implements it differently.

但我想这不是你真正想要的吗?另外,请注意建议的逗号运算符。虽然这是一件好事,但它可能会在其他语言(如C 和 C++)中引起顽皮的副作用,因为 PHP 以不同的方式实现它。

回答by jlettvin

array_map(function($i) {
    echo "Var {$i} is ".(10-$i)."<br/>".PHP_EOL; 
}, range(1,10));

回答by Sean

To expand on the other (correct) answers, what you were doing is called nestingloops. This means that for every iteration of the outer loop (the first one), you were completing the entire inner loop. This means that instead of 11 outputs, you get 11 + 11 + 11 + ... = 11 * 11outputs

为了扩展其他(正确)答案,您所做的称为嵌套循环。这意味着对于外循环(第一个)的每次迭代,您都在完成整个内循环。这意味着您得到的不是 11 个输出,而是11 + 11 + 11 + ... = 11 * 11输出

回答by Chika Ugwuanyi

I tried to get a start and end time and store in the database, given a start and end time, you loop through each time using two variables i&j

我试图获取开始和结束时间并存储在数据库中,给定开始和结束时间,每次使用两个变量 i&j 循环

   $start = "09:00";
   $end = "18:00";
   $strEnTim = strtotime("10.00");

   $slotStart = strtotime($start);
   $slotEnd = strtotime($end);
   $slotNow = $slotStart;

   for( $i=$slotStart, $j=$strEnTim; $i, $j<=$slotEnd; $i+=3600,  $j+=3600) 
   {
        if(( $i < $slotNow) && ( $j < $strEnTim)) continue;
        Slot::create([
            'start_time' => date("H:i",$i),
            'end_time' => date("H:i", $j)
        ]);
   }