PHP unset vs array_pop?
Asked Answered
B

10

10

If I want to remove the last element of an array, I can use either of these two code:

  1. array_pop($array); (the return value is not used)

  2. unset($array[count($array) -1]);

Is there any performance or semantic difference between them?

If not, which is preferred?

Bordereau answered 30/7, 2013 at 12:39 Comment(4)
array_pop returns the popped element.Recha
I would think pop would be faster because there is no count call or subtraction going on. But i mean thats a micro optimization youll probably never notice. I think pop is more readable and more "understandable at a glance".Coimbra
@Coimbra if your array of a size that such a difference would be ever noticeable - think of reducing the array size. It will be real optimization, unlike just a feeling of it.Localize
unset($array[count($array) -1]); will not work with associative arrays, or enumerated arrays where there are gaps in the enumerationFrightened
L
12

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"
Lyman answered 30/7, 2013 at 12:42 Comment(0)
A
10

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.

Alvarez answered 30/7, 2013 at 12:44 Comment(0)
A
5

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.

Admire answered 30/7, 2013 at 12:42 Comment(1)
@Lix, yes I mean that point was pretty explicit.. you see the semicolon shows that that is the complete statement......Bordereau
R
2

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.

Renzo answered 30/7, 2013 at 12:43 Comment(0)
S
1

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.

Shealy answered 30/7, 2013 at 12:46 Comment(0)
B
1

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.

Bolin answered 26/3, 2014 at 14:6 Comment(0)
R
0

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.

Rancell answered 30/7, 2013 at 12:44 Comment(0)
B
0

Yes there is a difference

array_pop() will also return removed element eg: last element, and

unset() will not return any thing

Blancmange answered 30/7, 2013 at 12:44 Comment(0)
U
0

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);
Upstairs answered 30/7, 2013 at 12:47 Comment(2)
No no this creates a new array, that's not what I want.Bordereau
I forgot the return value to rewrite the arrayUpstairs
M
0

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:

unset

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.

array_pop

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.

Max answered 17/7, 2019 at 8:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.