Remove Last Item In An Array

no-email (Damien Seguy), Damien Seguy
Mon, 10 Mar 2025 00:00:00 +0000

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.