Checking during array iteration, if the current element is the last element

后端 未结 8 2014
迷失自我
迷失自我 2020-12-13 03:41

Please help me to translate this pseudo-code to real php code:

 foreach ($arr as $k => $v)
    if ( THIS IS NOT THE LAST ELEMENT IN THE ARRAY)
        doS         


        
8条回答
  •  时光取名叫无心
    2020-12-13 04:02

    My solution, also quite simple..

    $array = [...];
    $last = count($array) - 1;
    
    foreach($array as $index => $value) 
    {
         if($index == $last)
            // this is last array
         else
            // this is not last array
    }
    

提交回复
热议问题