How to determine the first and last iteration in a foreach loop?

前端 未结 20 1113
遇见更好的自我
遇见更好的自我 2020-11-22 16:45

The question is simple. I have a foreach loop in my code:

foreach($array as $element) {
    //code
}

In this loop, I want to r

20条回答
  •  囚心锁ツ
    2020-11-22 17:22

    foreach ($arquivos as $key => $item) {
       reset($arquivos);
       // FIRST AHEAD
       if ($key === key($arquivos) || $key !== end(array_keys($arquivos)))
           $pdf->cat(null, null, $key);
    
       // LAST
       if ($key === end(array_keys($arquivos))) {
           $pdf->cat(null, null, $key)
               ->execute();
       }
    }
    

提交回复
热议问题