According to the Manual, array_shift
removes the first element from the array.
I've seen a lot of criticism from the internet as to how to perform this function, since it reordered the entire index of the array with each removal, thus reallocating the entire array.
I found this graph that represents the difference of performasse between array_shift
and array_pop
(remove the last element of the array), which shows an absurd difference.
Ihaveseenalteratives,liketheexamplebelow,toavoidthe"performance problem" (because of reindexing the indices) as follows:
function array_first(&$array)
{
reset($array);
$key = key($array);
$value = $array[$key];
unset($array[$key]);
return $value;
}
-
Creating a "manual" alteration really is the best way to solve this problem?
-
What makes this function slow is it simply the reindexing of the elements, or are there other factors as well?