Get next element in foreach loop

前端 未结 9 564
猫巷女王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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 04:04

    if its numerically indexed:

    foreach ($foo as $key=>$var){
    
        if($var==$foo[$key+1]){
            echo 'current and next var are the same';
        }
    }
    

提交回复
热议问题