Sometimes you need to remove the last element from a PHP array and retrieve its value at the same time. For example, you might want to process tasks in reverse order, manage an undo history, or remove the most recently added item.
The array_pop() function performs both operations in a single step.
What Is array_pop() in PHP?
The array_pop() function removes the last element of an array and returns its value.
Its syntax is:
array_pop(array &$array): mixed
The function accepts one parameter:
$array— The array from which the last element will be removed.
The return value is the removed element, or null if the array is empty.
Important: Unlike many PHP array functions, array_pop() modifies the original array directly. The & in its signature indicates that the array is passed by reference.
Example 1: Removing the Last Element
Let's remove the last programming language from an indexed array.
<?php
$languages = ["PHP", "Python", "JavaScript"];
$removed = array_pop($languages);
echo "Removed: " . $removed . PHP_EOL;
print_r($languages);
Output:
Removed: JavaScript
Array
(
[0] => PHP
[1] => Python
)
The function removes "JavaScript" and returns it. The original $languages array now contains only two elements.
If you don't need the removed value, you can simply call:
array_pop($languages);
Example 2: Working with Associative Arrays
The function also works with associative arrays and arrays containing non-sequential numeric keys.
<?php
$products = [
"KB-101" => "Keyboard",
"MS-102" => "Mouse",
"MN-103" => "Monitor"
];
$removed = array_pop($products);
echo $removed . PHP_EOL;
print_r($products);
Output:
Monitor
Array
(
[KB-101] => Keyboard
[MS-102] => Mouse
)
The function removes the last element according to the array's current order, regardless of its key.
It returns only the removed value, not the key. If you need the last key before removal, retrieve it using array_key_last().
Removing the final entry does not automatically reindex the remaining elements.
Handling Empty Arrays and null Values
Calling array_pop() on an empty array returns null without producing an error.
<?php
$items = [];
var_dump(array_pop($items));
Output:
NULL
However, null can also be a valid array value. For example, removing the final element from ["Ready", null] also returns null.
To distinguish these situations, check whether the array contains elements before calling the function:
<?php
$items = ["Ready", null];
if ($items !== []) {
$removed = array_pop($items);
echo "An element was removed.";
}
This is especially important when your application must distinguish an empty collection from a successfully removed element containing null.
Practical Example: Processing Tasks Using a Stack
One common use of array_pop() is implementing a stack.
A stack follows the Last In, First Out (LIFO) principle: the most recently added item is processed first.
Imagine an application that stores pending jobs in an array. We'll process them in reverse order using array_pop().
Create a file named task-stack.php:
<?php
declare(strict_types=1);
$jobs = [
[
"id" => "JOB-101",
"task" => "Prepare report"
],
[
"id" => "JOB-102",
"task" => "Update catalog"
],
[
"id" => "JOB-103",
"task" => "Send notification"
]
];
// Process the most recently added job first.
echo "<h2>Processing Jobs</h2>";
echo "<ul>";
while ($jobs !== []) {
$job = array_pop($jobs);
$message = $job["id"] . ": " . $job["task"];
echo "<li>" . htmlspecialchars(
$message,
ENT_QUOTES,
"UTF-8"
) . "</li>";
}
echo "</ul>";
echo "<p>Remaining jobs: "
. count($jobs)
. "</p>";
Browser output:
Processing Jobs
JOB-103: Send notification
JOB-102: Update catalog
JOB-101: Prepare report
Remaining jobs: 0
The last job added to the array is processed first. Each iteration removes one job until the array becomes empty.
This technique is useful for simple in-memory stacks and undo histories. For persistent or concurrent background jobs, use a suitable database-backed queue or messaging system instead.
array_pop() vs Related Functions
| Function | Behavior |
|---|---|
array_pop() |
Removes and returns the last element. |
array_push() |
Adds elements to the end of an array. |
array_shift() |
Removes and returns the first element. |
array_last() |
Returns the last value without removing it (PHP 8.5+). |
array_key_last() |
Returns the last key without modifying the array. |
Use array_pop() when you want to remove an element. If you only need to read the final value, use a non-destructive alternative.
Common Mistakes
- Expecting the array to remain unchanged: The function removes an element from the original array.
- Confusing the last key with the last value:
array_pop()returns the value, not its key. - Ignoring empty arrays: An empty array returns
null, which may also be a valid element value. - Passing an array expression directly: Because the function requires an array variable by reference, assign an array expression to a variable before passing it.
- Assuming the internal pointer is preserved:
array_pop()resets the input array's internal pointer.
Key Takeaways
array_pop()removes and returns the last array element.- It works with indexed and associative arrays.
- The original array is modified directly.
- An empty array returns
null. - The removed element is determined by array order, not by the largest key.
- It is useful for implementing Last In, First Out (LIFO) operations.
The array_pop() function provides a straightforward way to remove and retrieve the final element of a PHP array. It is especially useful when managing stacks, processing collections in reverse order, or removing recently added items.
For additional details, see the official PHP array_pop() documentation.