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.
🌐
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.
Top answer
1 of 16
3648

There are different ways to delete an array element, where some are more useful for some specific tasks than others.

Deleting a Single Array Element

If you want to delete just one single array element you can use unset() and alternatively array_splice().

By key or by value?

If you know the value and don't know the key to delete the element you can use array_search() to get the key. This only works if the element doesn't occur more than once, since array_search() returns the first hit only.

unset() Expression

Note: When you use unset() the array keys won’t change. If you want to reindex the keys you can use array_values() after unset(), which will convert all keys to numerically enumerated keys starting from 0 (the array remains a list).

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
          // ↑ Key of element to delete

Example Output:

[
    [0] => a
    [2] => c
]

array_splice() Function

If you use array_splice() the (integer) keys will automatically be reindex-ed, but the associative (string) keys won't change — as opposed to array_values() after unset(), which will convert all keys to numerical keys.

Note: array_splice() needs the offset, not the key, as the second parameter; offset = array_flip(array_keys(array))[key].

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
array_splice($array, 1, 1);
                  // ↑ Offset of element to delete

Example Output:

[
    [0] => a
    [1] => c
]

array_splice(), same as unset(), take the array by reference. You don’t assign the return values back to the array.

Deleting Multiple Array Elements

If you want to delete multiple array elements and don’t want to call unset() or array_splice() multiple times you can use the functions array_diff() or array_diff_key() depending on whether you know the values or the keys of the elements to remove from the array.

array_diff() Function

If you know the values of the array elements which you want to delete, then you can use array_diff(). As before with unset() it won’t change the keys of the array.

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = array_diff($array, ["a", "c"]);
                         // └────────┘
                         // Array values to delete

Example Output:

[
    [1] => b
]

array_diff_key() Function

If you know the keys of the elements which you want to delete, then you want to use array_diff_key(). You have to make sure you pass the keys as keys in the second parameter and not as values. Keys won’t reindex.

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_diff_key($array, [0 => "xy", "2" => "xy"]);
                              // ↑           ↑
                              // Array keys of elements to delete

Example Output:

[
    [1] => b
]

If you want to use unset() or array_splice() to delete multiple elements with the same value you can use array_keys() to get all the keys for a specific value and then delete all elements.

array_filter() Function

If you want to delete all elements with a specific value in the array you can use array_filter().

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_filter($array, static function ($element) {
    return $element !== "b";
    //                   ↑
    // Array value which you want to delete
});

Example Output:

[
    [0] => a
    [2] => c
]
2 of 16
1429

It should be noted that unset() will keep indexes untouched, which is what you'd expect when using string indexes (array as hashtable), but can be quite surprising when dealing with integer indexed arrays:

$array = array(0, 1, 2, 3);
unset($array[2]);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [3]=>
  int(3)
} */

$array = array(0, 1, 2, 3);
array_splice($array, 2, 1);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(3)
} */

So array_splice() can be used if you'd like to normalize your integer keys. Another option is using array_values() after unset():

$array = array(0, 1, 2, 3);

unset($array[2]);
$array = array_values($array);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(3)
} */
🌐
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.