How to Remove an Element from an Array in PHP: From Beginner to Pro
Imagine: you're writing an online store, and a customer removes an item from the cart. Or you're processing logs and need to discard unnecessary entries. Or you're simply cleaning an array of "junk" data. Sooner or later, every PHP developer faces the task: how to remove an element from an array?
It seems simple, right? But that's where the interesting part begins. PHP doesn't have a single "remove element" function. Instead, there are several methods, each with its own nuances. And if you choose the wrong one, you can end up with gaps in the array (broken indexing), lose data, or simply get confused.
In this article, we'll cover all the main ways to remove elements from an array in PHP: from simple unset() to advanced array_filter(). You'll learn when to use which method, and you'll never be afraid of this task again.
1. Quick Removal by Key: The unset() Function
The most obvious and popular method is the unset() function. It destroys a variable or an array element. It works instantly and is intuitively clear.
<?php$fruits = ['apple', 'banana', 'cherry', 'date'];unset($fruits[2]); // Remove element at index 2 ('cherry')print_r($fruits);?>Result:
Array( [0] => apple [1] => banana [3] => date)Important nuance: array keys are not re-indexed. Index 2 disappears, and now you have a "gap". For associative arrays, this is normal. But for lists (numeric arrays), this can be a problem if you expect continuous numbering (0, 1, 2, 3...).
When to use unset()?
- You know the exact key of the element.
- You don't care about the numbering (associative array).
- You need to remove one or two elements.
2. Removal by Value: array_search + unset
But what if you don't know the key, but you know the value? For example, you need to remove the item "banana" from the cart. This is where array_search() comes to the rescue.
<?php$cart = ['apple', 'banana', 'cherry', 'banana'];$key = array_search('banana', $cart);if ($key !== false) { unset($cart[$key]);}print_r($cart);?>Result:
Array( [0] => apple [2] => cherry [3] => banana)Note: array_search() finds the first occurrence. If there are multiple "bananas" in the array, only the first one will be removed. To remove all, you'll need to use a loop or array_diff() (more on that below).
3. Removal with Re-indexing: array_splice()
If you need to remove an element from a numeric array and want the keys to become continuous (0, 1, 2...), use array_splice(). This is a powerful function that can not only remove but also insert elements.
<?php$numbers = [10, 20, 30, 40, 50];array_splice($numbers, 2, 1); // Remove 1 element starting at index 2 (the number 30)print_r($numbers);?>Result:
Array( [0] => 10 [1] => 20 [2] => 40 [3] => 50)The array "collapsed" — the indices were recalculated. Perfect for lists, queues, and any sequential data.
Syntax of array_splice()
array_splice(array &$array, int $offset, ?int $length = null, mixed $replacement = []): array$array— the original array (passed by reference, will be modified).$offset— the starting index.$length— how many elements to remove.$replacement