PHP: How do you remove an element from an array?
If you want to remove a particular element from a PHP array with consecutive numeric indices, there are a few things to watch out for. There are also several ways to go about it.
The important thing to keep in mind is that PHP arrays are really a data structure that is a mixture of (Java) arrays and (Java) hash maps.
You should also remember that in PHP arrays both the keys and the values can have any data type, and that type can vary from entry to entry:
<?php
$array = [0 => "a", "one" => 1, 2 => "c"];
print_r($array);
?>
Array
(
[0] => a
[one] => 1
[2] => c
)
For the examples that follow, this PHP array is our starting point:
<?php
$array = [
0 => "a",
1 => "b",
2 => "c"
];
?>
Removing a single array element
Removing with the unset() function
<?php
$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
$array = array_values($array);
print_r($array);
?>
Output:
Array
(
[0] => a
[1] => c
)
When you use unset() the array keys are left untouched. To have the elements indexed consecutively and in ascending order again, you then also have to apply the array_values() function.
Incidentally, trying to access the element with index 2 in the example above (after the call to unset()) would result in an error.
Removing with the array_splice() function
<?php
$array = [0 => "a", 1 => "b", 2 => "c"];
array_splice($array, 1, 1);
print_r($array);
?>
Output:
Array
(
[0] => a
[1] => c
)
The array_splice() function automatically ensures that the array elements are indexed consecutively and in ascending order.
Removing several array elements
Removing with the array_filter() function
<?php
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_filter($array, function($key) {
if ($key != 0 && $key != 1) {
// Only elements for which the callback returns
// "true" become part of the new array.
return true;
}
}, ARRAY_FILTER_USE_KEY);
$array = array_values($array);
print_r($array);
?>
Output:
Array
(
[0] => c
)
The array_filter() function lets you apply a callback to every single array element:
function($key) {
if ($key != 0 && $key != 1) {
return true;
}
}
Only if this callback returns true does the array element become part of the
new array. The ARRAY_FILTER_USE_KEY flag makes sure that only the key, and not
the value, of the array element is passed to the callback.
With array_filter() too, the array keys are left untouched. To have the elements indexed consecutively and in ascending order again, you then also have to apply the array_values() function.
Removing with the array_diff_key() function
<?php
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_diff_key($array, [0 => "", 1 => ""]);
$array = array_values($array);
print_r($array);
?>
Output:
Array
(
[0] => c
)
With the array_diff_key() function the array keys are likewise left untouched. To have the elements indexed consecutively and in ascending order again, you then also have to apply the array_values() function.
About Netcup (advertisement)
The German host Netcup offers, among other things, affordable and powerful web hosting packages, KVM-based root servers and dedicated servers. With our voucher codes you can save even more (6€ off your first order, 30% off all KVM-based root servers, ...).