-5
 for ($i=1; $i<=500; $i++) 
 {
     echo "The number is " . $i . "<br />";
 }

好吧,这是 for 循环,但我想要下一个输出:

This number is 1 is Red
This number is 2 is blue
This number is 3 is green
This number is 4 is orange
This number is 5 is Red
This number is 6 is blue
This number is 7 is green
This number is 8 is orange
This number is 9 is Red
This number is 10 is blue
This number is 11 is green
This number is 12 is orange

- 基本上每四次重复红色、蓝色、绿色、橙色 - 这是脑筋急转弯。

4

3 回答 3

2

这也是给你的预告片

echo "<pre>";
$li = new LimitIterator(new InfiniteIterator(new ArrayIterator(array("red","blue","green","orange"))), 0, 500);
foreach ( $li as $key => $teaser ) {
    printf("The number is %d %s %s", $key + 1, $teaser, PHP_EOL);
}

观看现场演示

于 2012-10-07T13:23:36.543 回答
0

使用模函数根据计数器值决定要使用的颜色:

$colors=array ('red','blue','green','orange');
for ($i=1; $i<=500; $i++)
{
    $color=$colors[$i%4];
    echo "This number is ".$i." is ".$color."<br />\n";
}
于 2012-10-07T13:21:06.483 回答
0
$colours = ["Red", "Blue", "Green", "Orange"];

for ($i=0; $i<500; $i++)
{
    $colour = $i%4;
    echo "The number is " . $i . " is ".$colours[$colour]. "<br />";
}
于 2012-10-07T13:21:24.180 回答