Get next element in foreach loop

前端 未结 9 551
猫巷女王i
猫巷女王i 2020-11-30 03:56

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 cu

9条回答
  •  再見小時候
    2020-11-30 04:19

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

提交回复
热议问题