What’s the best way to get the last element of an array without deleting it?

Try

$myLastElement = end($yourArray);

Link to manual.

Or a PHP >= 7.3.0 solution:

(Which unlike end(...), does not change internal iterator.)

$myLastElement = $yourArray[array_key_last($yourArray)];

Link to manual

Leave a Comment