dongwen7371 2013-03-11 08:28
浏览 66
已采纳

在for循环中重新循环数组项[关闭]

I have a for loop that checks for conditions and I want to know if I can re-run an item through the loop if it doesn't meet the condition after I make some changes to variables. is it possible to add something to my else statement that will allow me to re-run the same $item under new conditions?

$array_1 = array();
$arraynumber = 0;
foreach($test_array as $item) {
     if($item[2] == $test_array[$y][2]) {
          $array_1[$arraynumber][] = $item;
          $z++;
     } else {
          $arraynumber++;
          $y = $y+$z;
          $z=0;

          echo $item[2];
     }
}

The solution turns out to be this, just for those interested:

foreach($test_array as $item) {
if($item[2] == $test_array[$y][2]) {
  $array_1[$arraynumber][] = $item;
 $z++;
}elseif($item[2] == $test_array[$y+$z][2]){
$array_1[$arraynumber+1][] = $item;

 $y = $y+$z;
  $z=1;

  $arraynumber++;
}else{
$array_1[$arraynumber+2][] = $item;
 $z++;
 $y = $y+$z;
  $z=0;
  $arraynumber++;

echo $item[2];
}
}
  • 写回答

5条回答 默认 最新

  • dpw43061 2013-03-11 08:33
    关注

    re-run an item through the loop

    You could use prev() which will set the internal data pointer 1 step back.

    prev($test_array); //will re-run the current element in the next iteration
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?