Introduction
Working with arrays is a fundamental aspect of programming in PHP. There are various scenarios where you might need to remove elements from an array, whether it’s a single element or multiple ones based on certain conditions. This tutorial will explore different techniques to effectively delete elements from an array in PHP using built-in functions like unset()
, array_splice()
, array_diff()
, array_filter()
, and others.
Deleting Single Array Elements
Using unset()
The unset()
function is straightforward for removing a single element when you know the index (key) of that element. It works by simply unsetting the specified key in an array:
$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
print_r($array);
Output:
Array
(
[0] => a
[2] => c
)
Note: The keys remain unchanged, meaning the numerical indices are not re-indexed.
Using array_splice()
If you prefer to remove an element and automatically reindex numeric array keys, array_splice()
is ideal:
$array = [0 => "a", 1 => "b", 2 => "c"];
array_splice($array, 1, 1);
print_r($array);
Output:
Array
(
[0] => a
[1] => c
)
array_splice()
adjusts the array so that numeric keys are sequentially reindexed.
Using array_search()
with unset()
If you know the value but not the key of an element, use array_search()
to find the key first:
$array = [0 => "a", 1 => "b", 2 => "c"];
$key = array_search("b", $array);
if ($key !== false) {
unset($array[$key]);
}
print_r($array);
Output:
Array
(
[0] => a
[2] => c
)
Deleting Multiple Array Elements
Using array_diff()
To remove multiple elements based on their values, use array_diff()
:
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_diff($array, ["a", "c"]);
print_r($array);
Output:
Array
(
[1] => b
)
Using array_filter()
This function allows for removing elements that match a specific condition:
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_filter($array, static function ($element) {
return $element !== "b";
});
print_r($array);
Output:
Array
(
[0] => a
[2] => c
)
Using array_diff_key()
To remove elements by their keys:
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_diff_key($array, [0 => "", 2 => ""]);
print_r($array);
Output:
Array
(
[1] => b
)
Re-indexing Arrays
If using unset()
, reindex numerical keys with array_values()
:
$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
$array = array_values($array);
print_r($array);
Output:
Array
(
[0] => a
[1] => c
)
Alternatively, array_merge()
can be used for re-indexing while preserving associative keys:
$array = ["a", "b", "c"];
$array = array_merge($array);
unset($array[1]);
print_r($array);
Output:
Array
(
[0] => a
[1] => c
)
Conclusion
Removing elements from arrays in PHP can be accomplished through various methods, each suited for different scenarios. Whether you need to remove a single element by key or multiple ones based on values or keys, PHP provides robust functions that simplify these operations. Understanding when and how to use unset()
, array_splice()
, array_diff()
, and others is essential for effective array manipulation.