Return the first or last element in an array without deleting it

Posted 2 years ago

Returning the last item

To return the last item in an array you can simply call the “end” function below.


$array = array( 'a','b','c');
echo end($array); // will return "c"
var_dump($array);

More on the PHP function end() here.

Return the first item

Just like the end() function, we have available to us the “current()” function.


$array = array( 'a','b','c');
echo current($array); // will return "a"
var_dump($array);

More on the PHP function current() here.