使用foreach循环下一 [英] Using next in foreach loop

查看:127
本文介绍了使用foreach循环下一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过一个数组使用的foreach循环。

I am looping through an array using foreach.

在特定的情况下,我需要知道下一个元素的值之前迭代来了(如prediction)元素。对于我计划用函数 next()的

In a particular situation I need to know the value of the next element before iteration comes to that(like a prediction) element. For that I am planning to use the function next().

在文档我只注意到,明年()推进内部数组指针向前发展。

In documentation I just noticed that next() advances the internal array pointer forward.

next()的行为就像电流(),有一个区别。它推进
  内部数组指针一个地方向前返回元素之前
  值。这意味着它返回下一个数组值和推进
  通过一个内部数组指针。

next() behaves like current(), with one difference. It advances the internal array pointer one place forward before returning the element value. That means it returns the next array value and advances the internal array pointer by one.

如果这样会影响我的foreach循环?

If so will it affect my foreach loop?

推荐答案

这不会影响你的循环
如果以这种方式来使用它。

It will not affect your loop if you use it in this way

<?php

$lists = range('a', 'f');

foreach($lists as &$value) {
   $next = current($lists);
   echo 'value: ' . $value . "\n" . 'next: ' . $next . "\n\n";
}

输出

值:一个
  接下来是b

value: a next: b

值:B
  下一:C

value: b next: c

值:C
  接下来:D

value: c next: d

值:D
  接下来:电子

value: d next: e

值:电子
  接下来:F

value: e next: f

值:F
  下一篇:

value: f next:

这篇关于使用foreach循环下一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆