PHP Remove Array Items
In this page:
unset($array[key]); // remove one item
array_pop($array); // remove the last
array_shift($array); // remove the first
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.
Example: Removing a Specific Item with unset()
<?php
// Declare `$arr` as an array: `["a", "b", "c"]`
$arr = ["a", "b", "c"];
// Remove `$arr[1]`
unset($arr[1]);
// Print a human-readable dump of `$arr`
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.
Example: Removing the Last Item with array_pop()
<?php
// Declare `$stack` as an array: `["a", "b", "c"]`
$stack = ["a", "b", "c"];
// Declare `$last`, set to `array_pop($stack)`
$last = array_pop($stack);
// Print `$last . "\n"` to the output
echo $last . "\n";
// Print a human-readable dump of `$stack`
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.
Example: Removing the First Item with array_shift()
<?php
// Declare `$queue` as an array: `["a", "b", "c"]`
$queue = ["a", "b", "c"];
// Declare `$first`, set to `array_shift($queue)`
$first = array_shift($queue);
// Print `$first . "\n"` to the output
echo $first . "\n";
// Print a human-readable dump of `$queue`
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.
Example: Removing Items That Match a Condition with array_filter()
<?php
// Declare `$numbers` as an array: `[1, 2, 3, 4, 5]`
$numbers = [1, 2, 3, 4, 5];
$odd = array_filter($numbers, function ($n) {
// Return `$n % 2 !== 0` from this function
return $n % 2 !== 0;
});
// Print a human-readable dump of `$odd`
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.
Example: Removing All Items and Emptying an Array
<?php
// Declare `$cart` as an array: `["item1", "item2"]`
$cart = ["item1", "item2"];
// Declare `$cart` as an empty array
$cart = [];
// Print a human-readable dump of `$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: