【发布时间】:2012-02-28 22:34:30
【问题描述】:
我遇到了一个 foreach 问题,他的主要建议是检查服务器是否已启动,然后访问该文件,而我被困在一个 foreach 上,我想让它重复一个步骤。例如我有以下代码:
<?php
$Array = array("one","two","tree","four");
next($Array);
$i = 0;
foreach($Array as $Key=>$Value)
{
$i++;
echo "Key=".$Key." VAL=".$Value."<br>";
if($Key==2) prev($Array);
if($i==10) break;
}
?>
正在输出:
Key=0 VAL=one
Key=1 VAL=two
Key=2 VAL=tree
Key=3 VAL=four
我想让它输出
Key=0 VAL=one
Key=1 VAL=two
Key=2 VAL=tree
Key=2 VAL=tree
Key=2 VAL=tree
Key=2 VAL=tree
Key=2 VAL=tree
Key=2 VAL=tree
Key=2 VAL=tree
Key=2 VAL=tree // till the $i is 10 therefore it's exits the foreach trough break;
我已经尝试过 prev($Array);没有结果。
我也在考虑
while(true)
{
if(server exists)
{
echo "server is good";
break;
}
else
{
echo "server is BAD ... continueing";
prev($Array);
}
}
但这对我也不起作用... 谁能帮我解决这个问题?
【问题讨论】:
标签: php for-loop foreach iteration