dongliang9682
dongliang9682
2009-07-23 15:22
浏览 166
已采纳

Php for循环有2个变量?

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

var 1 is 9

var 2 is 8 ...

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • dtysql0586
    dtysql0586 2009-07-23 15:24
    已采纳

    Try this:

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

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

    点赞 评论
  • dongzhuang5741
    dongzhuang5741 2009-07-23 15:24

    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.

    Edit: Thanks Gumbo for catching the inclusion for me.

    点赞 评论
  • duanchi19820419
    duanchi19820419 2009-07-23 15:28

    To expand on the other (correct) answers, what you were doing is called nesting loops. 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 * 11 outputs

    点赞 评论
  • drl37530
    drl37530 2009-07-23 15:41

    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.

    点赞 评论
  • dongxuanchao1425
    dongxuanchao1425 2015-03-20 14:01
    array_map(function($i) {
        echo "Var {$i} is ".(10-$i)."<br/>".PHP_EOL; 
    }, range(1,10));
    
    点赞 评论
  • doumi2106
    doumi2106 2016-01-07 17:06

    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>";
    }
    
    点赞 评论
  • dongyu9850
    dongyu9850 2016-06-25 15:03

    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

       $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)
            ]);
       }
    
    点赞 评论

相关推荐