PHP - Quickly get the first and last elements of a foreach loop
Posted on Tuesday, June 26th (4459 days ago)
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
Joseph R. B. Taylor is a humble designer/developer who makes stuff for screens of all shapes and sizes. He is currently the lead UI/UX Architect at MScience, LLC, where he works to create simple experiences on top of large rich datasets for their customers and clients.