Remove Last Item In An Array

../_images/remove_last_item.png

Three ways to remove the last item in an array.

array_pop() is the most adapted function, as it does actually that.

unset() is dedicated to removing elements, though it requires the calculation of the last key before. It is still the fastest of all three.

array_slice() works with the negative offset, just like a string. It is not too much slower, until one realize its result needs to be reassigned.

All in all, they need a good million iterations to see actual performances differences. It is a micro-optimisation.

See Also

PHP Features