Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: array_pop - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

array_product" width="11" height="7"/> <array_pad
Last updated: Fri, 02 May 2008

view this page in

array_pop

(PHP 4, PHP 5)

array_pop — 配列の末尾から要素を取り除く

説明

mixed array_pop ( array &$array )

array_pop() は配列 array の最後の値を取り出して返します。 配列 array は、要素一つ分短くなります。 array が空 (または、配列でない) の場合、 NULL が返されます。

注意: この関数は、 配列 (array) ポインタを使用した後にリセット (reset()) します。

パラメータ

array

値を取り出す配列。

返り値

配列 array の最後の値を取り出して返します。 array が空 (または、配列でない) の場合、 NULL が返されます。

例1 array_pop() の例

<?php
$stack 
= array("orange""banana""apple""raspberry");
$fruit array_pop($stack);
print_r($stack);
?>

この後、$stack の要素は三つのみとなります。

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

そして、raspberry$fruit に代入されます。

参考

array_push()array_shift() および array_unshift() も参照ください。



array_product" width="11" height="7"/> <array_pad
Last updated: Fri, 02 May 2008
 
add a note add a note User Contributed Notes
array_pop
sonetti at hotmail dot com
05-Feb-2008 06:15
@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.
Orsi
11-Jan-2008 12:05
Hi,

Here is a simple function which delete one element from the array (with value):
<?php
/*
* This function deletes the given element from a one-dimension array
* Parameters: $array:    the array (in/out)
*             $deleteIt: the value which we would like to delete
*             $useOldKeys: if it is false then the function will re-index the array (from 0, 1, ...)
*                          if it is true: the function will keep the old keys
* Returns true, if this value was in the array, otherwise false (in this case the array is same as before)
*/
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...
smp_info at yahoo dot com
26-Nov-2007 08:44
I'm sorry, I must have been extremely tired when writing the note below.  :P

Replace $element with $array, and the code will work nicely.

The right code would be..

<?php
//$array = array('one', 'two', 'three', 'four');

//pop the last element off and return the array
$array = array_diff($array, array(array_pop($array)));

//$array = array('one', 'two', 'three');
smp_info at yahoo dot com
20-Nov-2007 09:23
I've found myself several times wanting the array (minus the popped element) returned, instead of the element returned.

The following code does this nicely:

<?php
//$array = array('one', 'two', 'three', 'four');

//pop the last element off and return the array
$array = array_diff($array, array(array_pop($element)));

//$array = array('one', 'two', 'three');
doyley3731 at gmail dot com
31-Oct-2007 09:37
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;
}
rmondragon at gmail dot com
08-Jun-2005 06:03
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
<?
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 ;)
16-Dec-2004 01:29
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.
eddie at metafoundry dot com
26-Nov-2004 01:35
Quick way to get the extension from a file name using array_pop:

$ext = array_pop(explode(".",$filename));
31-Mar-2004 06:55
A function to delete an array value that recalculates the index ( its very short and easy to understand ).
Hope this might help someone...

<?php
/* Usage:
    $array : Array
    $indey : Integer
   
    The value of $array at the index $index will be
    deleted by the function.
*/
function array_trim ( $array, $index ) {
   if (
is_array ( $array ) ) {
      unset (
$array[$index] );
     
array_unshift ( $array, array_shift ( $array ) );
      return
$array;
      }
   else {
      return
false;
      }
   }
?>
Alex Dowgailenko
15-Dec-2003 08:36
array_pop() can be usefull for fetching extentions of files, especially in cases where there might be more than one period in the filename.

eg:

<?php
$filename
= "textfile.txt.bak";
$tmp = explode(".", $filename);
$ext = array_pop($tmp);

print_r($ext); // Shows "bak"
?>
22-Oct-2003 05:46
Be aware that using array_pop on an associative array that uses a numeric string as a key changes the key:

<?php
$stack
= array("12" => "green", "54" => "brown", "672" => "blue");
print_r($stack);
$fruit = array_pop($stack);
print_r($stack);
?>

Results of execution:
Array
(
    [12] => green
    [54] => brown
    [672] => blue
)
Array
(
    [0] => green
    [1] => brown
)

However, if there is a non-numeric character in the key, the key will be maintained:

<?php
$stack
= array("g1" => "green", "b1" => "brown", "b2" => "blue");
print_r($stack);
$fruit = array_pop($stack);
print_r($stack);
?>

Results of execution:
Array
(
    [g1] => green
    [b1] => brown
    [b2] => blue
)
Array
(
    [g1] => green
    [b1] => brown
)
ryan8613(at)hotmail(dot)com
08-Jun-2003 10:10
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.
Alex Chacn
01-Mar-2003 10:16
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;
}
?>

array_product" width="11" height="7"/> <array_pad
Last updated: Fri, 02 May 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites