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