6
According to the Manual, array_shift
removes the first element of the array.
I have seen many criticisms on the Internet about the performance of this function, because it reorder the whole index of the array at each removal, thus relocating the whole array.
I found this graph that represents the difference in performance between array_shift
and array_pop
(removes the last element of the array), which shows an absurd difference.
I have seen amendments, such as the example below, to avoid the "performance problem" (because of reindexation of inflows) as follows:
function array_first(&$array)
{
reset($array);
$key = key($array);
$value = $array[$key];
unset($array[$key]);
return $value;
}
Creating an alterative "manual" is really the best way to solve this problem?
What makes this function slow is simply the reindexation of the elements, or there are also other factors?
The function you put me in seems to be the behavior of
array_pop
, not ofarray_shift
...– Oeslei
Corrected by @Oeslei
– Wallace Maxters
I consider that if you have an array with 1000 positions, the problem is in the implementation and below that the
array_pop
is performing similarly according to the chart. Still, if you don’t need the reordering of indexes, the function you set would be a better alternative toarray_shift
.– Oeslei
You already have
reset
, friend.– Wallace Maxters