Alex Rivera | Logout

Get next element in foreach loop

Asked 2011-02-23T20:38:39.157
69

I have a foreach loop and I want to see if there is a next element in the loop so I can compare the current element with the next. How can I do this? I've read about the current and next functions but I can't figure out how to use them.

Edit
Report

1 Answer

31

You could probably use while loop instead of foreach:

while ($current = current($array) )
{
    $next = next($array);
    if (false !== $next && $next == $current)
    {
        //do something with $current
    }
}
answered 2011-10-21T13:16:09.587

Your Answer