PHP unset vs array_pop?

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

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

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

Is there any kind of performance or semantic difference between the two?

If not, what is preferred?

+8
source share
10 answers

unset not suitable if you need to "do" something with a deleted value (unless you previously assigned it to something else), since it does not return anything, whereas array_pop will give you what was the last element.

The unset option you provided may be slightly less efficient, since you count the length of the array and do some math, but I expect that the difference, if any, is not significant.

As others have said, this is true if your array is numerical and contiguous, however if the array is not structured this way, the material becomes complex

For instance:

 <?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" 
+10
source

array_pop($array) removes the last element of $array .

unset($array[count($array) -1]); deletes an element with index count($array) -1 . This element is not necessarily the last element of the array.

Consider $array = array(0 => 'foo', 2 => 'bar', 1 => 'baz') . In this case, $array[1] is the last element. Code

 foreach (array(0 => "foo", 2 => "bar", 1 => "baz") as $key => $value) echo "$key => $value\n"; 

prints

 0 => foo 2 => bar 1 => baz 

In addition, an element with index count($array) -1 may not even exist. There can be spaces in a set of indices, and whole indices can be mixed with string indices.

+10
source

The return values ​​are different. array_pop returns the last element, and unset returns nothing.

For simple removal of the last element, array_pop would be better because you do not need to do count($array)-1 , and it is cleaner and more readable.

+4
source

Yes there is.

Firstly, the unset() parameter will only work for adjacent numeric arrays. If your array contains elements that are not numerical or have any spaces in its numerical sequence, then the unset() call will get the wrong value from count() and fail.

Secondly, if your array is numeric and contiguous, there is still a difference:

array_pop() will also return the value of the popup element as the return value. unset() will not do this.

So, if you need to continue to use data, use array_pop() .

If you don’t need to save the value, then no, it’s probably not too important which one you use, I suspect array_pop() may be faster (due to the fact that you do not need to call count() ), but I don’t checked, and, frankly, if you do not make thousands of calls, the difference will be negligible.

+2
source

Except for the obvious differences in the call syntax and return value ...

array_pop always appears last. Your count - 1 disables the element by its numeric identifier, which only works as you would expect if all elements are indexed continuously.

+1
source

For what's worth it, using a bit of existing code that gets called more than 2,000 times in a race, I put in $ whatevers [] = $ whatever (parameter value) at the top and array_pop ($ whatevers) at the bottom.

The function calls itself recursively to about 7 or 8 levels and (of course), I made $ whatevers static, so the array grew and decreased.

Result? The difference between this code and the comments was immeasurable up to 100 thousand seconds on a Windows 7 laptop. This changed the fair bit due to other things, but for large runs the difference in average values ​​was meaningless.

The performance overhead of array_pop () is simply not worth the second thought, and although it cannot be theoretically faster, no one will ever be able to detect the difference.

+1
source

As others have noted, their functionality is the same; remove the return value from array_pop . However, it is also worth mentioning the possible performance problem of the unset method on a large array due to the call to count() .

As Oswald notes, it is also worth noting that unset() will work as expected on numeric keys.

0
source

Yes there is a difference

array_pop () also returns the deleted element, for example: the last element, and

unset () will not return anything

0
source

I would prefer unset (), but you call count (), which can consume performance.

An alternative is array_slice (array, offset, length, preserve_keys):

 $array = array_slice($array, 0, -1, true); 
0
source

Another consideration that should be taken into account is that if after deleting the last element you click on a new element, you get different results, in which the index at which the new element is placed:

disarmed

 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 can see, the new item 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 item is placed at index 2 . Maybe this is the most desirable behavior.

0
source

Source: https://habr.com/ru/post/950623/


All Articles