PHP already provides a large number of methods to manipulate arrays. The official PHP documentation shows a full list of array functions. Those can be used to create, sort and edit any of your arrays.
Let’s assume you want to remove an item from an array. PHP provides multiple solutions out of the box. Let’s have a look at possible solutions.
unset()
The method unset()
can be used to remove a single element of the array. As this method requires the index of the element, we have to use array_search()
to find the index for a given value:
$myArray = ['apple', 'banana', 'cherry', 'date'];
$elementToRemove = 'banana';
$index = array_search($elementToRemove, $myArray);
if ($index !== false) {
unset($myArray[$index]); // Remove the element at $index
}
/*
[0] => 'apple'
[2] => 'cherry'
[3] => 'date'
*/
array_splice()
You can use this function to delete a section of an array and replace it with another value. As this method also requires the index of the element, we have to use array_search()
to find the index for a given value. To remove a single item you can simply specify a length of 1:
$myArray = ['apple', 'banana', 'cherry', 'date'];
$elementToRemove = 'banana';
$index = array_search($elementToRemove, $myArray);
if ($index !== false) {
array_splice($myArray, $index, 1);
}
/*
[0] => 'apple'
[1] => 'cherry'
[2] => 'date'
*/
array_diff()
You can use this function to generate an array containing the elements from the first array that do not exist in the other arrays.
$myArray = ['apple', 'banana', 'cherry', 'date'];
$elementToRemove = 'banana';
$myArray = array_diff($myArray, [$elementToRemove]);
/*
[0] => 'apple'
[2] => 'cherry'
[3] => 'date'
*/
array_filter()
This function can be used with a callback function to filter elements from an array:
$myArray = ['apple', 'banana', 'cherry', 'date'];
$elementToRemove = 'banana';
$myArray = array_filter($myArray, function($value) use ($elementToRemove) {
return $value !== $elementToRemove;
});
/*
[0] => 'apple'
[2] => 'cherry'
[3] => 'date'
*/
Re-Indexing the results array
You might notice that some of the solutions keep the index-to-value relation of their items. Means, the indices of the manipulated array do not change. If you need to re-index the array to remove the gap (and have continuous indices), you can use the array_values()
method:
$myArray = array_values($myArray); // Re-index the array if you want to remove the gap
/*
before:
[0] => 'apple'
[2] => 'cherry'
[3] => 'date'
after:
[0] => 'apple'
[1] => 'cherry'
[2] => 'date'
*/
Performance Comparison
When comparing the performance of those methods, we get the following result:
Method | Removing item | With Re-Indexing |
---|---|---|
unset() | 0.171 µs | 0.310 µs |
array_splice() | 0.335 µs | 0.335 µs (not necessary) |
array_diff() | 0.253 µs | 0.380 µs |
array_filter() | 0.828 µs | 0.914 µs |
In this case, the unset()
method provides the fastest results. array_splice()
is also a good alternative, as it does not require any re-indexing after the item removal.
For this test, the mean execution time in a loop with 1,000,000 cycles was calculated for each solution. Please note that this test uses the arrays from the examples above. For more complex data, the results might be different.
Leave a Reply