suche nach in der

array_product> <array_pad
Last updated: Fri, 18 May 2012

view this page in

array_pop

(PHP 4, PHP 5)

array_popLiefert das letzte Element eines Arrays

Beschreibung

mixed array_pop ( array &$array )

array_pop() liefert den letzten Wert von array, und verkürzt array um ein Element. Ist array leer (oder kein Array), wird NULL zurückgegeben. In letzterem Fall wird auch eine Warnung erzeugt.

Hinweis: Diese Funktion setzt (reset()) nach Verwendung den array-Zeiger zurück.

Parameter-Liste

array

Das Array, aus dem der Wert geholt werden soll.

Rückgabewerte

Gibt den letzten Wert von array zurück. Wenn array leer ist (oder kein Array), wird NULL zurückgegeben.

Beispiele

Beispiel #1 array_pop()-Beispiel

<?php
$stack 
= array("Orange""Banane""Apfel""Himbeere");
$fruit array_pop($stack);
print_r($stack);
?>

Danach hat $stack nur 3 Elemente:

Array
(
    [0] => Orange
    [1] => Banane
    [2] => Apfel
)

und Himbeere wird $fruit zugewiesen.

Siehe auch

  • array_push() - Fügt ein oder mehr Elemente an das Ende eines Arrays
  • array_shift() - Liefert ein Element vom Beginn eines Arrays
  • array_unshift() - Fügt ein oder mehr Elemente am Anfang eines Arrays ein



add a note add a note User Contributed Notes
array_pop
rmondragon at gmail dot com
07-Jun-2005 11: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 ;)
15-Dec-2004 05: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
25-Nov-2004 05:35
Quick way to get the extension from a file name using array_pop:

$ext = array_pop(explode(".",$filename));
30-Mar-2004 11: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 12: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"
?>
21-Oct-2003 10: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 03: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 Chacón
01-Mar-2003 02: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> <array_pad
Last updated: Fri, 18 May 2012