foreach($array as $element) {
if ($element === reset($array))
echo 'FIRST ELEMENT!';
if ($element === end($array))
echo 'LAST ELEMENT!';
}
Blog
PHP - Quickly get the first and last elements of a foreach loop
June 26, 2012
From the archive. I've left this one just as I wrote it.