Signature
array_pop(array &$array): mixedPurpose
array_pop() removes the last element from an array and hands that element back to you. It changes the array you pass in, so the array ends up one element shorter. It is the standard way to treat an array as a stack (last in, first out): you add items to the end and take them off the end.
The function works on whatever element is physically last in the array's order. That is not necessarily the element with the highest key, although in a normal list the two are the same.
Signature
The & means the parameter is taken by reference. The function modifies the caller's variable directly instead of working on a copy.
Parameters and defaults
$array: the array to pop from. It is required and has no default. Because it is passed by reference, you must supply a variable. A literal array cannot be passed, and the result of a function call is not a proper target either.
There are no other parameters, so there is nothing to configure. You cannot pop more than one element at a time or choose a position.
Return value and failure cases
The return type is mixed, because the popped value can be of any type: string, integer, nested array, object and so on.
- If the array has elements, you get the value of the last one.
- If the array is empty, you get
null. - If you pass something that is not an array, PHP throws a
TypeError.
The function does not report failure in any other way. An empty array is not an error, it simply yields null.
As a side effect, the manual notes that the internal array pointer of the input array is reset after use. Code that relies on current(), next() or key() on the same array should not assume the old position survives.
Example 1: removing the last item of a list
The most common use is taking the final item off a plain list and keeping the rest.
$stack = ["orange", "banana", "apple", "raspberry"];
$fruit = array_pop($stack);
echo json_encode($fruit), "\n";
echo json_encode($stack, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";"raspberry" ["orange","banana","apple"]
The returned value is "raspberry", and $stack now holds the other three fruits in their original order.
Example 2: associative arrays
With string keys, array_pop() still takes the last element in order. The key is discarded together with the element, and you only receive the value.
$user = ["id" => 7, "name" => "Ada", "role" => "admin"];
$last = array_pop($user);
echo json_encode(["popped" => $last, "rest" => $user], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"popped":"admin","rest":{"id":7,"name":"Ada"}}
If you need the key as well, read it first with array_key_last() before popping.
Example 3: empty arrays and stored null values
An empty array returns null. But an array whose last element is itself null also returns null, so the return value alone cannot tell the two cases apart.
$empty = [];
$withNull = [1, null];
$a = array_pop($empty);
$b = array_pop($withNull);
echo json_encode([$a, $b, count($empty), count($withNull)]), "\n";[null,null,0,1]
Both calls return null, yet the second array shrank from two elements to one. When a stored null is possible, check count() or compare with [] before popping.
Edge cases
- Empty array: returns
nulland leaves the array empty. - Stored null: a popped
nullis indistinguishable from the empty case without a separate check. - Nested arrays: the popped value can itself be an array, and it is returned whole.
- String keys: only the value is returned, and the key disappears from the array.
- Numeric keys: existing keys are not renumbered. In a list with keys 0, 1 and 2, popping leaves keys 0 and 1.
Common mistakes
- Expecting a new array back. The return value is the removed element, not the shortened array. The shortened array is the variable you passed in.
- Passing a non-variable. Because of the by-reference parameter,
array_pop([1, 2, 3])is an error. Store the array in a variable first. - Treating null as proof of emptiness. Check
count($array)first whennullis a legitimate stored value. - Using it to remove the first element. That job belongs to
array_shift(). Popping always works at the end. - Popping inside a loop that also reads the array pointer. The pointer reset can change the behavior of
current()andnext()based loops. - Passing a value that may not be an array. A
nullor string argument throws aTypeError, so validate input from outside sources.
Related functions
array_push(): appends one or more elements to the end of an array.array_shift(): removes and returns the first element.array_unshift(): prepends one or more elements.array_key_last(): gets the last key without modifying the array.end(): reads the last value without removing it.
Recap
array_pop() takes a variable holding an array, removes its last element and returns that element. It returns null for an empty array, which is also what you get when the stored last value is null, so check the count when the difference matters. It works with associative arrays by discarding the key, and it resets the internal pointer. Use array_shift() for the front of the array and array_push() to add to the end.
Tested examples
$stack = ["orange", "banana", "apple", "raspberry"];
$fruit = array_pop($stack);
echo json_encode($fruit), "\n";
echo json_encode($stack, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
"raspberry" ["orange","banana","apple"]
$user = ["id" => 7, "name" => "Ada", "role" => "admin"];
$last = array_pop($user);
echo json_encode(["popped" => $last, "rest" => $user], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
{"popped":"admin","rest":{"id":7,"name":"Ada"}}
$empty = [];
$withNull = [1, null];
$a = array_pop($empty);
$b = array_pop($withNull);
echo json_encode([$a, $b, count($empty), count($withNull)]), "\n";
[null,null,0,1]