How to Delete Elements From an Array in PHP?

Coding (Php 7.x)


5 array functions that are going to make your developer's life easier
 
/img/blog/how-to-delete-elements-from-an-array-in-php.jpeg

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
]

 

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
]

 

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
]

 

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
]

 

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
]

 

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

 
 
If you like this content and you are hungry for some more join the Facebook's community in which we share info and news just like this one!

Other posts that might interest you

Coding (Php 7.x) Nov 5, 2020

Introduction to Composer [all about repositories]

See details
Coding (Php 7.x) Nov 17, 2022

How to Refactor Legacy Code

See details
Coding (Php 7.x) Nov 17, 2022

This Script Turned 2 Days Into 8 Seconds

See details
Get my free books' review to improve your skill now!
I'll do myself