Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Array Functions : array_pop

array_pop

Pop the element off the end of array (PHP 4, PHP 5)
mixed array_pop ( array &array )

Example 269. array_pop() example

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

After this, $stack will have only 3 elements:

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

and raspberry will be assigned to $fruit.


Related Examples ( Source code ) » array_pop






Code Examples / Notes » array_pop

15-dec-2004 04: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

Quick way to get the extension from a file name using array_pop:
$ext = array_pop(explode(".",$filename));


rmondragon

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 ;)


doyley3731

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;
}


21-oct-2003 09: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
)


alex dowgailenko

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"
?>


alex chac├│n

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;
}
?>


30-mar-2004 10: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;
     }
  }
?>


ryan8613

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.


Change Language


Follow Navioo On Twitter
array_change_key_case
array_chunk
array_combine
array_count_values
array_diff_assoc
array_diff_key
array_diff_uassoc
array_diff_ukey
array_diff
array_fill_keys
array_fill
array_filter
array_flip
array_intersect_assoc
array_intersect_key
array_intersect_uassoc
array_intersect_ukey
array_intersect
array_key_exists
array_keys
array_map
array_merge_recursive
array_merge
array_multisort
array_pad
array_pop
array_product
array_push
array_rand
array_reduce
array_reverse
array_search
array_shift
array_slice
array_splice
array_sum
array_udiff_assoc
array_udiff_uassoc
array_udiff
array_uintersect_assoc
array_uintersect_uassoc
array_uintersect
array_unique
array_unshift
array_values
array_walk_recursive
array_walk
array
arsort
asort
compact
count
current
each
end
extract
in_array
key
krsort
ksort
list
natcasesort
natsort
next
pos
prev
range
reset
rsort
shuffle
sizeof
sort
uasort
uksort
usort
eXTReMe Tracker