©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
(PHP 4, PHP 5, PHP 7)
array_pop — 将数组最后一个单元弹出(出栈)
&$array
) array_pop() 弹出并返回
array
数组的最后一个单元,并将数组
array
的长度减一。如果
array
为空(或者不是数组)将返回 NULL
。 此外如果被调用不是一个数则会产生一个
Warning。
Note: 使用此函数后会重置( reset() ) array 指针。
array
需要做出栈的数组。
返回 array
的最后一个值。如果 array
是空(如果不是一个数组),将会返回 NULL
。
Example #1 array_pop() 例子
<?php
$stack = array( "orange" , "banana" , "apple" , "raspberry" );
$fruit = array_pop ( $stack );
print_r ( $stack );
?>
经过此操作后, $stack 将只有 3 个单元:
Array ( [0] => orange [1] => banana [2] => apple )
并且 rasberry 将被赋给 $fruit 。
[#1] Anonymous [2015-11-06 16:02:39]
Let's "do stuff" with array_pop()
<?php
$dependecyInjection = ['globalState' => 'isHorrible'];
$workQueue = array_reverse($workQueue); // O(n)
// While we have stuff to do..... = [[ {function}, $params.... ], [], []...]
while (!empty($workQueue)) {
// O(1) : And resize array. (maybe also memory size ?)
$work = array_pop($workQueue);
// If not `muted`, invoke the worker function....
if (isset($work[0])) {
$processor = array_shift($work);
if ($moreWork = $processor->__invoke($work, $dependecyInjection)) { // <== Do stuff.
// O(n) : add new stuff to the END of array ! (FIFO)
$outputCommands += array_reverse($moreWork);
}
}
}
[#2] Anonymous [2014-02-28 08:21:45]
Strict Standards will be thrown out if you put exploded array in array_pop:
<?php
$a = array_pop(explode(",", "a,b,c"));
echo $a;
?>
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.
[#3] elad dot yosifon at gmail dot com [2013-08-10 18:45:44]
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.
[#4] qeremy [2012-03-07 11:26:08]
For the sake of array_unshift()
:)
<?php
function array_unpop(&$arr) {
$args = func_get_args(); unset($args[0]);
$tarr = array();
foreach ($args as $arg) {
$tarr[] = $arg;
}
$arr = array_merge($arr, $tarr);
}
$queue = array("orange", "banana");
array_unpop($queue, "apple", "raspberry");
print_r($queue);
?>
Array
(
[0] => orange
[1] => banana
[2] => apple
[3] => raspberry
)
[#5] Igor Feghali [2009-02-04 09:18:34]
Beware that array_pop() returns a copy of the element (even if the array element is a reference). That means you will get different results on PHP 4 and 5 when dealing with objects.
<?php
$a = new stdClass();
$a->foo = 'bar';
$arr = array(&$a);
$b = array_pop($arr);
$b->foo = 'baz';
var_dump($a);
?>
Output of PHP 5.2.8:
object(stdClass)#1 (1) {
["foo"]=>
string(3) "baz"
}
Output of PHP 4.4.9:
object(stdClass)(1) {
["foo"]=>
string(3) "bar"
}
For more information please refer to:
http://br2.php.net/manual/en/language.oop5.references.php
[#6] mcgroovin at gmail dot com [2008-10-23 12:13:12]
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.
<?php
function multipleArrayIntersect($arrayOfArrays, $matchKey)
{
$compareArray = array_pop($arrayOfArrays);
foreach($compareArray AS $key => $valueArray){
foreach($arrayOfArrays AS $subArray => $contents){
if (!in_array($compareArray[$key][$matchKey], $contents)){
unset($compareArray[$key]);
}
}
}
return $compareArray;
}
?>
[#7] sonetti at hotmail dot com [2008-02-05 01:15:53]
@smp_info
I think you are still tired. What would be wrong with:
<?php
$array = array('one', 'two', 'three', 'four');
//pop the last element off
array_pop($array);
//$array == array('one', 'two', 'three');
?>
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.
[#8] Orsi [2008-01-10 07:05:27]
Hi,
Here is a simple function which delete one element from the array (with value):
<?php
function deleteFromArray(&$array, $deleteIt, $useOldKeys = FALSE)
{
$tmpArray = array();
$found = FALSE;
foreach($array as $key => $value)
{
if($value !== $deleteIt)
{
if(FALSE === $useOldKeys)
{
$tmpArray[] = $value;
}
else
{
$tmpArray[$key] = $value;
}
}
else
{
$found = TRUE;
}
}
$array = $tmpArray;
return $found;
}
?>
Maybe it will help somebody...
[#9] doyley3731 at gmail dot com [2007-10-31 04:37:25]
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;
}
[#10] rmondragon at gmail dot com [2005-06-07 14:03:58]
In a previous example ...
<?php
function array_trim ( $array, $index ) {
if ( is_array ( $array ) ) {
unset ( $array[$index] );
array_unshift ( $array, array_shift ( $array ) );
return $array;
}
else {
return false;
}
}
?>
This have a problem. if u unset the last value and then use
<?php
array_unshift ( $array, array_shift ( $array ) );
?>
will return a : Array ( [0] => )
so u can fix it using...
<?php
if (count($array) > 0) array_unshift ( $values, array_shift ( $values ) );
?>
good luck ;)
[#11] [2004-12-15 08:29:59]
strrchr is a lot more useful than the other example using array_pop for finding the extension of a file. For example:
<?php
$ext = strrchr($filename, ".");
?>
$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.
[#12] eddie at metafoundry dot com [2004-11-25 08:35:31]
Quick way to get the extension from a file name using array_pop:
$ext = array_pop(explode(".",$filename));
[#13] [2004-03-30 13:55:04]
A function to delete an array value that recalculates the index ( its very short and easy to understand ).
Hope this might help someone...
<?php
function array_trim ( $array, $index ) {
if ( is_array ( $array ) ) {
unset ( $array[$index] );
array_unshift ( $array, array_shift ( $array ) );
return $array;
}
else {
return false;
}
}
?>
[#14] ryan8613(at)hotmail(dot)com [2003-06-08 06:10:26]
A function that may help some out, considering it's pretty much the one mentioned previously...
<?php
function array_trim($arr, $indice) {
if(!isset($indice)) {
$indice = count($arr)-1;
}
unset($arr[$indice]);
array_shift($arr);
return $arr;
}
?>
It cuts the given index value off of the array, but without the shift, if the 'index' value isn't given, it cuts off the end value.
[#15] Alex Chac?n [2003-02-28 17:16:40]
alex.chacon@terra.com
Hi
Here there is a function that delete a elemente from a array and re calculate indexes
<?php
function eliminarElementoArreglo ($array, $indice)
{
if (array_key_exists($indice, $array))
{
$temp = $array[0];
$array[0] = $array[$indice];
$array[$indice] = $temp;
array_shift($array);
//reacomodamos ?ndices
for ($i = 0 ; $i < $indice ; $i++)
{
$dummy = $array[$i];
$array[$i] = $temp;
$temp = $dummy;
}
}
return $array;
}
?>