PHP - Quickly get the first and last elements of a foreach loop

foreach($array as $element) {
   
if ($element === reset($array))
        echo
'FIRST ELEMENT!';

   
if ($element === end($array))
        echo
'LAST ELEMENT!';
}

 


style="display:block"
data-ad-client="ca-pub-6216396065935707"
data-ad-slot="8941332981"
data-ad-format="auto">

 

 

About Joseph R. B. Taylor

is a designer/developer who makes stuff for screens of all shapes and sizes. He currently works at Edvisors, Inc. where he creates screen-based experiences for used by millions of college students every year.

Wanna Talk?

Contact Me