PHP Remove Array Items
In this page:
Removing a Specific Item with unset()
unset($arr["key"]) removes exactly one item by its key or index, deleting it from the array entirely -- but on an indexed array, this leaves a gap where that index used to be, since the remaining items are not automatically renumbered.
Note: If you need a clean, gap-free indexed array after removing an item, follow unset() with array_values() to renumber the remaining items.
Warning: unset() leaving a gap in numeric keys can break code elsewhere that assumes indexes are always sequential starting at 0.
Example: Removing a Specific Item with unset()
<?php
$arr = ["a", "b", "c"];
unset($arr[1]);
print_r($arr);
?>
Login to try C/C++/Java/PHP code in the editor
Removing the Last Item with array_pop()
array_pop($arr) removes the last item from an array and returns that removed value, modifying the original array directly -- a common pattern for treating an array like a stack, where the most recently added item is the first one processed back out.
Note: array_pop() naturally keeps indexed arrays gap-free, since it always removes from the very end rather than the middle.
Warning: Calling array_pop() on an already-empty array returns null rather than raising an error, so check the array is non-empty first if that distinction matters.
Example: Removing the Last Item with array_pop()
<?php
$stack = ["a", "b", "c"];
$last = array_pop($stack);
echo $last . "\n";
print_r($stack);
?>
Login to try C/C++/Java/PHP code in the editor
Removing the First Item with array_shift()
array_shift($arr) removes and returns the first item of an array, then automatically re-indexes every remaining numeric key downward by one -- useful for processing a queue where items should be handled in the order they were added.
Note: array_shift() automatically fixes numeric indexing after removal, unlike unset(), which is one reason to prefer it specifically for removing the first item.
Warning: array_shift() is slower than array_pop() on large arrays, since every remaining element's index has to shift down by one.
Example: Removing the First Item with array_shift()
<?php
$queue = ["a", "b", "c"];
$first = array_shift($queue);
echo $first . "\n";
print_r($queue);
?>
Login to try C/C++/Java/PHP code in the editor
Removing Items That Match a Condition with array_filter()
array_filter($arr, $callback) returns a brand-new array containing only the items for which $callback returns true, effectively filtering out (removing) everything that does not pass -- unlike the other removal functions here, it does not modify the original array.
Note: Reassign the result of array_filter() back to a variable (often the same one) since it returns a new array rather than modifying the original in place.
Warning: array_filter() preserves the original keys of the items that remain, which can leave gaps in numeric indexing -- pair it with array_values() if a clean re-indexed result is needed.
Example: Removing Items That Match a Condition with array_filter()
<?php
$numbers = [1, 2, 3, 4, 5];
$odd = array_filter($numbers, function ($n) {
return $n % 2 !== 0;
});
print_r($odd);
?>
Login to try C/C++/Java/PHP code in the editor
Removing All Items and Emptying an Array
Assigning an empty array literal, $arr = [], instantly clears out every item, resetting the variable to a fresh, empty array -- the simplest way to remove everything at once, such as clearing a cart after checkout completes.
Note: Clearing an array with $arr = [] is simpler and more common than looping and unset()-ing every individual item one at a time.
Warning: Reassigning $arr = [] creates a brand-new empty array; if other variables held a reference to the original array, they are not automatically cleared too.
Example: Removing All Items and Emptying an Array
<?php
$cart = ["item1", "item2"];
$cart = [];
print_r($cart);
?>
Login to try C/C++/Java/PHP code in the editor
- Using unset() on an indexed array and expecting the remaining items to automatically re-index -- unset() leaves a gap in the numeric keys instead.
- Confusing array_pop() (removes from the end) with array_shift() (removes from the start) -- using the wrong one removes the wrong item.
- Forgetting that array_filter() returns a NEW array with the matching items removed, rather than modifying the original array in place.
- unset($arr["key"]) removes a specific item by key or index, but can leave a gap in numeric indexing afterward.
- array_pop($arr) removes and returns the last item; array_shift($arr) removes and returns the first item.
- array_filter($arr, $callback) returns a new array containing only the items that pass a given test, effectively removing everything that fails it.
All array-removal functions discussed here have been part of PHP since its earliest array support.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: