If I want to remove the last element of an array, I can use either of these two code:
array_pop($array);
(the return value is not used)
Is there any performance or semantic difference between them?
If not, which is preferred?
If I want to remove the last element of an array, I can use either of these two code:
array_pop($array);
(the return value is not used)
Is there any performance or semantic difference between them?
If not, which is preferred?
unset
is no good if you need to "do" anything with the removed value (unless you have previously assigned it to something elsewhere) as it does not return anything, whereas array_pop
will give you the thing that was the last item.
The unset
option you have provided may be marginally less performant since you are counting the length of the array and performing some math on it, but I expect the difference, if any, is negligible.
As others have said, the above is true if your array is numerical and contiguous, however if you array is not structured like this, stuff gets complicated
For example:
<?php
$pop = $unset = array(
1 => "a",
3 => "b",
0 => "c"
);
array_pop($pop);
// array looks like this:
// 1 => "a", 3 => "b"
$count = count($unset) - 1;
unset($count);
// array looks like this because there is no item with index "2"
// 1 => "a", 3 => "b", 0 => "c"
array_pop($array)
removes the last element of $array
.
unset($array[count($array) -1]);
removes the element at index count($array) -1
. This element is not neccesarily the last element of the array.
Consider $array = array(0 => 'foo', 2 => 'bar', 1 => 'baz')
. In this case , $array[1]
is the last element. The code
foreach (array(0 => "foo", 2 => "bar", 1 => "baz") as $key => $value)
echo "$key => $value\n";
prints
0 => foo 2 => bar 1 => baz
Moreover, an element at index count($array) -1
might not even exist. There can be gaps in the set of indices and integer indices can be mixed with string indices.
The return values are different. array_pop
returns the last element, while unset
doesn't return anything.
For simply removing the last element, array_pop
would be better because you don't need to execute count($array)-1
, and it is cleaner and more readable.
Yes there is.
Firstly, the unset()
option will only work for numerical, contiguous arrays. If your array contains elements that are not numerical, or has any gaps in its numerical sequence, then the unset()
call will get the incorrect value from count()
and will fail.
Secondly, assuming your array is numerical and contiguous, there is still a difference:
array_pop()
will also give you back the value of the popped element as a return value. unset()
will not do this.
So if you need to keep using the data, use array_pop()
.
If you don't need to keep the value, then no, it probably doesn't matter too much which one you use, I suspect that array_pop()
may be faster (due to not needing to call count()
), but I haven't checked, and to be honest, unless you're doing thousands of calls, the difference will be negligible anyway.
Except for the obvious differences in call syntax and return value...
array_pop
always pops whatever is last.
Your count - 1
unsets an element by its numeric id, which only works as you expect it to if all elements are continuously numerically indexed.
For what it's worth, using a bit of existing code that gets called a bit over 2000 times in a run, I put in a $whatevers[]=$whatever (a parameter value) at the top and and array_pop($whatevers) at the bottom.
The function calls itself recursively down to about 7 or 8 levels and (of course) I made $whatevers static so the array grew and shrunk.
The result? The difference between this code in and commented out was unmeasurable down to 100ths of a second on a windows 7 laptop. It varied a fair bit because of other things, but over lots of runs the difference in the averages was meaningless.
The performance overhead of array_pop() just isn't worth a second thought and though unset might be theoretically faster, nobody will ever be able to detect the difference.
As others have mentioned - their functionality is the same, bar the return value from array_pop
. However, it's also worth mentioning the possible performance issue of the unset
method on a large array due to the count()
call.
As Oswald mentions, it is also worth noting that unset()
will only be working as expected on numeric keys.
Yes there is a difference
array_pop() will also return removed element eg: last element, and
unset() will not return any thing
I would prefer unset() but you call count() which can consume performance.
An alternative choice is array_slice(array, offset, length, preserve_keys) :
$array = array_slice($array, 0, -1, true);
Another consideration to take into account is that if after deleting the last item you push a new element, you get different results in which the index the new element is placed at:
php > $a = [1,2,3];
php > var_dump($a);
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
php > unset($a[2]);
php > var_dump($a);
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
php > $a[] = 5;
php > var_dump($a);
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[3]=>
int(5)
}
As you see, the new element is placed at index 3
instead of 2
.
php > $a = [1,2,3];
php > var_dump($a);
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
php > array_pop($a);
php > var_dump($a);
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
php > $a[] = 5;
php > var_dump($a);
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(5)
}
Now the new element is placed at index 2
. Maybe this is the most desirable behaviour.
© 2022 - 2024 — McMap. All rights reserved.
unset($array[count($array) -1]);
will not work with associative arrays, or enumerated arrays where there are gaps in the enumeration – Frightened