PHP跟踪最后的x次迭代

时间:2015-12-07 21:01:09

标签: php loops for-loop foreach

我有四个变量:def get_c_attribute(self): return self.o_category.c_attribute $one_flag$two_flag$three_flag。我在for循环中使用它们作为标志,因为我想跟踪最后四次迭代。

$four_flag

现在它打印$one_flag = 1; $two_flag = 0; $three_flag = 0; $four_flag = 0; $a = array(1, 2, 3, 17, 27, 44, 45, 47, 49); foreach ($a as $v) { if ($one_flag){ $first_v = $v; $one_flag = 0; $two_flag = 1; } if ($two_flag){ $second_v = $v; $two_flag = 0; $three_flag = 1; } if ($three_flag){ $third_v = $v; $three_flag = 0; $four_flag = 1; } if ($four_flag){ $fourth_v = $v; $four_flag = 0; $first_flag = 1; } if ($v == 45){ # tricky part print "The last three v's were: " . $first_v . ", " . $second_v . ", " . $third_v . "\n"; } } 但它应该打印The last three v's were 1, 1, 1。此外,上面代码的问题是我需要知道哪个标志等于The last three v's were 45, 44, 27(我们当前正在使用什么标志),以便我可以打印正确的语句。

例如,当1时,$v == 45假设等于27;假设$first_v等于44;假设$second_v等于17;并且假设$fourth_v等于45.我需要知道third_v等于1才能按顺序打印出“最后三个是$ second_flag,$ first_flag,$ fourth_flag”。

如何让循环工作?如何跟踪最后四次迭代?

编辑:我错过了。我其实想要打印$three_flag

5 个答案:

答案 0 :(得分:2)

我认为这是一个最小的例子,真正的程序更复杂。否则,您可以获取数组的长度并根据它检索最后3个元素。

我将改变方法如下:

<map center="{{polyline[0][0]}},{{polyline[0][1]}}" zoom="15" style="width:100%; height: 500px;">
  <marker ng-repeat="pos in polyline" position="{{pos[0]}}, {{pos[1]}}" icon="{{pos[2]}}" title="{{(pos[3] | date:'EEE, MM-dd-yyyy HH:mm:ss:sss')}}">
  </marker>  
  <shape name="polyline" path="{{polyline}}" geodesic="true" stroke-color="#0000FF", stroke-opacity="1.0" stroke-weight="3"></shape>
</map>  

我使用-1作为指示,没有为变量分配任何内容。如果需要,您应该根据您的上下文进行调整。

答案 1 :(得分:1)

它有助于您了解您尝试解决的问题,但是我还是使用圆形阵列来跟踪最后四个问题。 (根据您的修订版编辑):

$a = array(1, 2, 3, 17, 27, 44, 45, 47, 49);
$previous = array ();
$pCounter = 0;

foreach ($a as $i => $v) {
    if (45 === $v) {
        print_r(array_reverse($previous));
    }

    // track the last three using a circular array
    $previous[$pCounter] = $v;
    $pCounter = ($pCounter + 1) % 3;
}

结果:

Array
(
    [0] => 44
    [1] => 27
    [2] => 17
)

Try it online.

答案 2 :(得分:1)

问题在于你的if语句。

//loop sets $v = 1
//this one is true due to initial parameters, execute
if ($one_flag){
    $first_v = $v; //$first_v = 1
    $one_flag = 0;
    $two_flag = 1; //$two_flag is now TRUE!!!
}
//$two_flag is true, execute immediately!
if ($two_flag){
    $second_v = $v; //$second_v = 1
    $two_flag = 0;
    $three_flag = 1;
}

虽然在我的阅读中,这应该会使印刷品给出The last three v's were 45, 45, 45。如果您想继续这样做,则需要使用else if

答案 3 :(得分:1)

$a = array(1, 2, 3, 17, 27, 44, 45, 47, 49);

$track = array();

$num = 3;

foreach ($a as $v){

    $track[] = $v;

    if (count($track) > $num){
        $track = array_slice($track, 1);
    }

    if ($v == 45){
        print_r(array_reverse($track));
    }

}

结果

Array
(
    [0] => 45
    [1] => 44
    [2] => 27
)

答案 4 :(得分:0)

而不是如果使用 elseif ,它将满足您的目的,并将变量名称 $ first_flag 更改为 $ one_flag < /强>

    $one_flag = 1;
    $two_flag = 0;
    $three_flag = 0;
    $four_flag = 0;
    $probe_counter = 1;
     $first_v=0;
     $second_v=0;
     $third_v=0;$fourth_v=0;

    $a = array(1, 2, 3, 17, 27, 44, 45, 47, 49);

    foreach ($a as $key => $val) {
        if ($one_flag){
            $first_v = $val;
            $one_flag = 0;
            $two_flag = 1;
        }
        elseif ($two_flag){
            $second_v = $val;
            $two_flag = 0;
            $three_flag = 1;
        }
        elseif ($three_flag){
            $third_v = $val;
            $three_flag = 0;
            $four_flag = 1;
        }
        elseif ($four_flag){
            $fourth_v = $val;
            $four_flag = 0;
            $one_flag = 1;
        }

        if ($val == 45){
            # tricky part
            echo "The last three v's were: " . $first_v . ", " . $second_v . ", " . $third_v . "<br>";
        }
    }