Hướng dẫn array pop php

[PHP 4, PHP 5, PHP 7, PHP 8]

array_popPop the element off the end of array

Description

array_pop[array &$array]: mixed

Note: This function will reset[] the array pointer of the input array after use.

Parameters

array

The array to get the value from.

Return Values

Returns the value of the last element of array. If array is empty, null will be returned.

Examples

Example #1 array_pop[] example

After this, $stack will have only 3 elements:

Array
[
    [0] => orange
    [1] => banana
    [2] => apple
]

and raspberry will be assigned to $fruit.

See Also

  • array_push[] - Push one or more elements onto the end of array
  • array_shift[] - Shift an element off the beginning of array
  • array_unshift[] - Prepend one or more elements to the beginning of an array

elad dot yosifon at gmail dot com

9 years ago

Notice:
the complexity of array_pop[] is O[1].
the complexity of array_shift[] is O[n].
array_shift[] requires a re-index process on the array, so it has to run over all the elements and index them.

noreply at i-asm dot com

1 year ago

Note that array_pop doesn't issue ANY warning or error if the array is already empty when you try to pop something from it. This is bizarre! And it will cause cascades of errors that are hard to resolve without knowing the real cause.

Rather than an error, it silently returns a NULL object, it appears, so in my case I ended up with warnings elsewhere about accessing elements of arrays with invalid indexes, as I was expecting to have popped an array. This behaviour [and the lack of any warning, when many trivial things are complained about verbosely] is NOT noted in the manual above. Popping an already empty stack should definitely trigger some sort of notice, to help debugging.

Sure, it's probably good practice to wrap the pop in an if [count[$array]] but that should be suggested in the manual, if there's no error returned for trying something that should fail and obviously isn't expected to return a meaningful result.

mcgroovin at gmail dot com

13 years ago

I wrote a simple function to perform an intersect on multiple [unlimited] arrays.

Pass an array containing all the arrays you want to compare, along with what key to match by.

rmondragon at gmail dot com

17 years ago

In a previous example ...


This have a problem. if u unset the last value and then use

will return a :  Array [ [0] => ]
so u can fix it using...



good luck ;]

Alex Chac?n

19 years ago


Hi
Here there is a function that delete a elemente from a array and re calculate indexes

doyley3731 at gmail dot com

14 years ago

I had a problem when using this function because my array was made up entirley of numbers, so I have made my own function.  Hopefully it will be useful to somebody.

function array_trim_end[$array]{

$num=count[$array];
$num=$num-1;
unset[$array[$num]];

return $array;
}

qeremy

10 years ago

For the sake of array_unshift[]
:]



Array
[
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
]

Anonymous

8 years ago

Strict Standards will be thrown out if you put exploded array in array_pop:


You will see:
PHP Strict Standards:  Only variables should be passed by reference in - on line 2

Strict Standards: Only variables should be passed by reference in - on line 2
c

Notice that, you should assign a variable for function explode, then pass the variable reference into array_pop to avoid the Strict Standard warning.

sonetti at hotmail dot com

14 years ago

@smp_info
I think you are still tired. What would be wrong with:



As the documentation clearly notes, array_pop[] not only returns the last element, but actually removes it from the array wich is passed by reference. Calling array_diff is a waste of resources.

Orsi

14 years ago

Hi,

Here is a simple function which delete one element from the array [with value]:


Maybe it will help somebody...

Anonymous

17 years ago

strrchr is a lot more useful than the other example using array_pop for finding the extension of a file. For example:



$ext will contain the extension of the file, including a ".", if the file has an extension, and FALSE if the file has no extension. If the file has multiple extensions, such as "filename.tar.gz", then this construction will just return the last extension.

Chủ Đề