6

I am looping through an array using foreach.

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() 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.

If so will it affect my foreach loop?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Quicksilver
  • 2,546
  • 3
  • 23
  • 37
  • @Jonas while you are editing all of these tags are you considering the possibility of closing duplicates? I think I've seen some dupe closure candidates in your page stirring wake. – mickmackusa May 12 '23 at 12:17

3 Answers3

10

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";
}

OUTPUT

value: a next: b

value: b next: c

value: c next: d

value: d next: e

value: e next: f

value: f next:

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
liyakat
  • 11,825
  • 2
  • 40
  • 46
  • Awesome find. I hope it is stable and forward_compatible. And yes it can be useful. :-) – e-motiv Dec 31 '14 at 16:17
  • 1
    Should the line `$next = current($lists);` be `$next = next($lists);`? – Warren Mar 11 '17 at 05:55
  • No it's not, you can run this code to your local php environment and try – liyakat Mar 11 '17 at 10:35
  • 4
    @e-motiv **This is not stable and forward-compatible.** It isn't even *backward*-compatible. As tested with http://sandbox.onlinephpfunctions.com/ this always states "a" as being next in PHP 5.0.4 to 5.1.6. Then from 5.2.16 to 5.6.29 its "b", "c", "d", "e", "f" (so your output) and from 7.0.1 onwards it stays "a". As pointed out in http://grokbase.com/t/php/php-internals/13bjdgjhcz/foreach-and-current#20131118q6fneqrepzxtdk562h7p2wkdmm `foreach` and `current` have nothing to do with each other. – flu May 18 '18 at 08:26
3

next() doesn't affect foreach(), period.

In PHP 7.2 at least,

$values = ['a', 'b', 'c', 'd', 'e'];

foreach ($values as $value) {
  next($values);
  $two_ahead = next($values);
  echo("Two ahead: $two_ahead\n");
  echo("Current value: $value\n");
}

Produces:

Two ahead: c
Current value: a
Two ahead: e
Current value: b
Two ahead: 
Current value: c
Two ahead: 
Current value: d
Two ahead: 
Current value: e

Also note that the foreach loop does not affect the position of next, either. They're independent.

If you have an array with sequential numeric keys (the default), then ops' answer is best for what you're trying to do. I merely answered the question.

mlncn
  • 3,308
  • 2
  • 23
  • 20
1

Try this code:

$a=$array();
foreach($a as $key=>$var)
{
   if(isset($a[$key+1]))
      echo $a[$key+1];//next element
}
ops
  • 2,023
  • 18
  • 21
  • This assumes integer indexed array with contiguous keys, which of course arrays in PHP need not be either. If you wish to safely use this method with unknown arrays or non-integer keys then iterate the result of `array_values($a)`. – Dan Feb 02 '21 at 03:42