Check out array_slice() http://php.net/manual/en/function.array-slice.php

Last argument true is to preserve keys.

When you pass the offset as negative, it starts from the end. It's a nice trick to get last elements without counting the total.

$array = [
    "a" => 1,
    "b" => 2,
    "c" => 3,
];

$lastElementWithKey = array_slice($array, -1, 1, true);

print_r($lastElementWithKey);

Outputs:

Array
(
    [c] => 3
)
Answer from Mārtiņš Briedis on Stack Overflow
🌐
PHP
php.net › manual › en › function.array-pop.php
PHP: array_pop - Manual
<?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; } ?> ... 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.
🌐
W3Schools
w3schools.com › php › func_array_pop.asp
PHP array_pop() Function
MySQL Database MySQL Connect MySQL ... Order By MySQL Delete Data MySQL Update Data MySQL Limit Data · PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat Parser PHP DOM Parser · AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll · PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Server PHP Syllabus PHP Study Plan PHP Certificate ... array() array_change_key_case() ...
Top answer
1 of 5
43
// first
$value = reset($arr);
$key = key($arr);

(in that order)

See reset()PHP Manual, key()PHP Manual.

unset(key]); # in case you want to remove it.

However array_pop()PHP Manual is working with the last element:

// last
$value = end(key = key($arr);
unset(key]); # in case you want to remove it.

See end()PHP Manual.

For the fun:

[$value, $key] = [reset(arr)]; // first
[$value, arr), key($arr)]; // last

(PHP 7.1+)

or

list($value, $key) = array(reset(arr)); // first
list($value, $key) = array(end(arr)); // last

(PHP 4.3+)

or

extract(array('value' => reset(arr))); // first
extract(array('value' => end(arr))); // last

(PHP 4.3+; Caution: extract() in use!)

or

// first
reset($arr);
list(value) = each($arr);


// last
end($arr);
list(value) = each($arr);

(Note: The each() function is deprecated since PHP 7.2.0 and gone since PHP 8.0.0)

or whatever style of play you like ;)

Dealing with empty arrays

It was missing so far to deal with empty arrays. So it's a need to check if there is a last (first) element and if not, set the $key to null (as null can not be an array key):

// first
for ($key = null, $value = null; false !== $_ = reset($arr);)
{
    $value = $_;
    unset(key = key($arr)]);
    break;
}
unset($_);

// last
for ($key = null, $value = null; false !== arr);)
{
    $value = $_;
    unset(key = key($arr)]);
    break;
}
unset($_);

This will give for a filled array like $arr = array('first' => '1st', 'last' => '2nd.');:

string(4) "2nd." # value
string(4) "last" # key
array(1) { # leftover array
  ["first"]=>
  string(3) "1st"
}

And an empty array:

bool(false) # value
NULL # key
array(0) { # leftover array
}

Afraid of using unset?

In case you don't trust unset() having the performance you need (of which I don't think it's really an issue, albeit I haven't run any metrics), you can use the native array_pop() implementation as well (but I really think that unset() as a language construct might be even faster):

// first
reset(key = key(value = array_pop($arr);


// last
end(key = key(value = array_pop($arr);
2 of 5
7

array_slice

$arr = array('k1' => 'v1', 'k2' => 'v2', 'k3' => 'v3');

$a = array_slice($arr, 0, 1);
var_dump(arr = array_slice($arr, 1);
var_dump($arr);


array(1) {
  ["k1"]=>
  string(2) "v1"
}
array(2) {
  ["k2"]=>
  string(2) "v2"
  ["k3"]=>
  string(2) "v3"
}
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
Pop and Shift Arrays With PHP: When to Use Each One | Envato Tuts+
December 31, 2021 - We use end() to move the array pointer to the last element. The key() function helps us get the name of the person, and array_pop() gets the name of the item while removing it from the array.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-array_pop-function
PHP array_pop() Function - GeeksforGeeks
June 20, 2023 - Examples: Input : $array = (1=>"ram", 2=>"krishna", 3=>"aakash"); Output : aakash Input : $array = (24, 48, 95, 100, 120); Output : 120 Below programs illustrate the array_pop() function in PHP: Example 1
🌐
w3resource
w3resource.com › php › function-reference › array_pop.php
PHP : array_pop() function - w3resource
key · krsort · list · natcasesort ... Last update on August 19 2022 21:50:40 (UTC/GMT +8 hours) The array_pop() function is used to remove the last element of an array....
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php array_pop
PHP array_pop() Function
April 6, 2025 - <?php $scores = [ "John" => "A", "Jane" => "B", "Alice" => "C" ]; $score = array_pop($scores); echo $score; print_r($scores);Code language: PHP (php) ... In this example, the array_pop() function remove the last element of the $scores array regardless of the key.
🌐
Phpzag
phpzag.com › manipulating-php-arrays-push-pop-shift-unshift
Manipulating PHP arrays: push, pop, shift, unshift – PHPZAG.COM
Array ( [0] => apple [1] => raspberry [2] => orange [3] => banana ) ... AI Tools AngularJS API Articles Code Snippets copilot Gemini HTML5 Interview Questions Javascript JQuery Laravel Magento MYSQL Payment Gateways PHP Tutorials vscode Wordpress
Find elsewhere
🌐
ZetCode
zetcode.com › php-array › array-pop
PHP array_pop - Remove Last Array Element in PHP
The last key-value pair ('age' => 30) is removed. The function works the same way with associative arrays as with indexed arrays. Shows what happens when array_pop is called on an empty array. ... <?php $emptyArray = []; $result = array_pop($emptyArray); var_dump($result); echo count($emptyArray); When called on an empty array, array_pop returns NULL. The array remains empty and no error is generated. Demonstrates processing array elements by repeatedly removing the last one.
🌐
Dev Lateral
devlateral.com › home › guides › php › how does array_pop work in php
How does array_pop work in PHP | Dev Lateral
May 14, 2024 - Array pop or array_pop() as it will be typically written in PHP code takes an array of data and returns back the last item in the array, whilst removing it from the array itself, therefore shortening the array by one element.