DEV Community

anastasionico
anastasionico

Posted on

How to delete element from an array in PHP?

Hi, I am Nico,

I am a Senior Engineer for a ticket broker based in London.

From the office! is a daily (almost) posting routine that I keep to journal about the tech things I have been doing in my day-to-day office life.

Deleting a single array element

There are various methods to delete an array element.

Some are more practical for some specific reasons than others.

unset()

If we want to delete just one element of the array we can use the unset method.

When we use unset() the array keys won't change.

If we want to reindex the keys we can use array_values() after the unset().

It will convert all keys to numerically enumerated keys starting from 0.

$nations = [0 => "Italy", 1 => "UK", 2 => "Spain"];
unset($nations[1]);
// It becomes[ [0] => Italy [2] => Spain]
Enter fullscreen mode Exit fullscreen mode

The unset() method takes the array by reference.\
What that means is that we don't give the return values of those functions back to the array but, instead, that will be done automatically.

array_splice()

If we want to use array_splice() the keys will automatically be reindexed, The associative keys won't change as opposed to array_values(), which will transform all keys to numerical keys.
array_splice() requires the offset, not the key, as the second parameter.

$nations = [0 => "Italy", 1 => "UK", 2 => "Spain"];

array_splice($nations, 1, 1);
// It becomes[[0] => Italy [2] => Spain]
Enter fullscreen mode Exit fullscreen mode

The array_splice() method also takes the array by reference.

How to delete multiple elements?

To delete multiple element at once we should use the functions array_diff() or array_diff_key().

Its choice depends on whether we know the values or the keys of the elements that need to be deleted.

array_diff()

If we know the values of the array elements that we want to delete, then we can use array_diff().

As before with unset() it won't alter the keys of the array.

$nations = [0 => "Italy", 1 => "UK", 2 => "Spain", 3 => "Portugal", 4 => "Spain"];
$nations = array_diff($nations, ["Italy", "Spain"]);
// It becomes[[1] => UK [3] => Portugal]
Enter fullscreen mode Exit fullscreen mode

array_diff_key()

If instead, we only know the keys of the elements we want to eliminate, We will use array_diff_key().
In this case, we pass the keys as keys in the second parameter and not as values.\
Again keys won't reindex.

$nations = [0 => "Italy", 1 => "UK", 2 => "Spain"];
$nations = array_diff_key($nations, [0 => "Portugal", "2" => "Germany"]);
// It becomes[[1] => UK]
Enter fullscreen mode Exit fullscreen mode

We can still use unset() or array_splice() for deleting many elements with the same value.

To do this we use array_keys() to get all the keys for a specific value and then delete all of the elements.

array_filter()

If we want to eliminate all elements with a distinct value we will opt for array_filter().

$nations = [0 => "Italy", 1 => "UK", 2 => "Spain"];
$nations = array_filter($nations, static function ($element) { return $element !== "UK";});
// It becomes[[0] => Italy [1] => Spain]
Enter fullscreen mode Exit fullscreen mode

If we know the value and don't know the key to delete the element we can use array_search() to get the key.

What is your favorite method?

I have used many of them on different occasions during the year and I believe unset() is still easier to use.

Comment below what you prefer to do write instead.

Conclusion

I have been a web developer for almost a decade now.

Working with world-class businesses and award-winning marketing agencies situated in the heart of London.

Also, I write articles and tutorials on my blog and online communities and help businesses build their presence online.

Click here to read more than 100+ of my blog post

(thanks to rizier123)

Top comments (1)

Collapse
 
anastasionico profile image
anastasionico

have this tips been useful?