How to Determine the Last and First Iteration of a For Each Loop PHP

A short snippet to determine the first and last iteration in a foreach loop for PHP.

Use this snippet to figure the Last and First iteration of a foreach loop. 

$i = 0;
$len = count($array);
foreach ($array as $item) {
    if ($i == 0) {
        // first
    } else if ($i == $len - 1) {
        // last
    }
    // …
    $i++;
}