array_pop

(PHP 4, PHP 5, PHP 7, PHP 8)

array_popDizinin sonundaki elemanı diziden çıkartır

Açıklama

function array_pop(array &$dizi): mixed

array_pop() dizi'deki son elemanı çıkartır ve son elemanın değerini döndürür, dizi bir eleman kısalır.

Bilginize:

Bu işlev diziyi kullandıktan sonra girdi dizisinin dahili göstericisini ilk elemana konumlandırır.

Bağımsız Değişkenler

dizi

Değer alınacak dizi.

Dönen Değerler

dizi'nin son elemanının değerini döndürür. Eğer dizi boş ise null değer döndürür.

Örnekler

Örnek 1 - array_pop() örneği

<?php
$depo = array("portakal", "muz", "elma", "ahududu");
$meyve = array_pop($depo);
print_r($depo);
?>

Bundan sonra, $depo üç elemana sahiptir:

Array
(
    [0] => portakal
    [1] => muz
    [2] => elma
)

Ve ahududu $meyve değişkenine atanmıştır.

Ayrıca Bakınız

add a note

User Contributed Notes 11 notes

up
83
elad dot yosifon at gmail dot com
13 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.
up
4
noreply at i-asm dot com
5 years 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.
up
5
mcgroovin at gmail dot com
17 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.

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