How can I Delete an Element from an Array in PHP?

Author — Nitish Kumar

Let’s say you have an array and you want to delete one or more elements from that array using PHP. Maybe you have to delete an element with a specific value or maybe you have to delete multiple consecutive elements. In this article, you will learn about different inbuilt PHP functions that can help you remove one or more elements based on their key or value. Here are a few variables that we are going to use:

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];

I have used the short array syntax in the code above which replaces array(your, elements) with [your, elements].

On This Page

Remove an Element from an Array by its Key Using unset()

The easiest way to remove or delete a single element from an array in PHP is to use the unset() function. This function is used to unset a given variable. In other words, you can use it to destroy any variable in your code.

To remove an array element by its key, provide the unset() function with the key of the element that you want to delete. In case of numeric arrays, this key will be the index of the element. Here is an example:

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];

unset($num_array[2]);
print_r($num_array);
/* Output —
Array
(
    [0] => Amanda
    [1] => John
    [3] => Megan
    [4] => Henry
) */

unset($asc_array["third"]);
print_r($asc_array);
/* Output —
Array
(
    [first] => Amanda
    [second] => John
    [fourth] => Megan
    [fifth] => Henry
) */

You should note that the keys of both the arrays remain unchanged after deleting the element from the arrays. If you want to reindex the keys after removing the element so that there is no gap, you can use the array_values() function. This function returns an indexed array of all the values in the supplied array.

Remember that array_values() will also change the keys of associative array to numerical indices. The following example will clear up how reindexing an array after removing an element works.

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];

unset($num_array[2]);
$reindexed = array_values($num_array);
print_r($reindexed);
/* Output —
Array
(
    [0] => Amanda
    [1] => John
    [2] => Megan
    [3] => Henry
) */

unset($asc_array["third"]);
$reindexed = array_values($asc_array);
print_r($reindexed);
/* Output —
Array
(
    [0] => Amanda
    [1] => John
    [2] => Megan
    [3] => Henry
) */

Remove an Element from an Array by its Value Using unset()

The only problem with the above solution is that you actually need to know the key to delete an element. Let’s say you only know the value of the element that you want to delete and have no idea about its key. In such cases, you can first use the array_search() function in PHP to get the key for a given value and then use unset() to delete the element.

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];

$key = array_search("John", $num_array);
unset($num_array[$key]);
print_r($num_array);
/* Output —
Array
(
    [0] => Amanda
    [2] => Kelly
    [3] => Megan
    [4] => Henry
) */

$key = array_search("John", $asc_array);
unset($asc_array[$key]);
print_r($asc_array);
/* Output —
Array
(
    [first] => Amanda
    [third] => Kelly
    [fourth] => Megan
    [fifth] => Henry
) */

As you saw in the above example, you can use array_search() and unset() together to remove or delete an array element by its value.

Using array_splice() to Delete Multiple Consecutive Elements from an Array

The array_splice() function in PHP is used to remove a portion of an array and replace it with something else. However, providing the replacement is optional and you can use this function to remove a portion of the array in one go. Here is an example:

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];
$fruits = ["Apple", "Mango", "Banana", "Lichi", "Pear", "Grapes", "Guava", "Papaya", "Orange"];

array_splice($num_array, 2, 1);
print_r($num_array);
/* Output —
Array
(
    [0] => Amanda
    [1] => John
    [2] => Megan
    [3] => Henry
) */

array_splice($asc_array, 2, 1);
print_r($asc_array);
/* Output —
Array
(
    [first] => Amanda
    [second] => John
    [fourth] => Megan
    [fifth] => Henry
) */

array_splice($fruits, 3, 4);
print_r($fruits);
/* Output —
Array
(
    [0] => Apple
    [1] => Mango
    [2] => Banana
    [3] => Papaya
    [4] => Orange
) */

Besides removing multiple elements at once, another advantage of this method is that the numeric keys were automatically re-indexed. At the same time, the keys of the associative array remained unchanged.

One important point that you should remember is that you have to pass the offset as second parameter and not the key. That’s why we did not pass the key third as a value of second array_splice() parameter when deleting elements from associative array. The third parameter is used to specify the number of elements starting from the offset that we want to remove. You can set it to 1 to remove a single element or set it to some other higher value to remove multiple elements.

Using array_diff() to Delete Multiple Elements by Their Value

While array_splice() can remove multiple elements from any at once, it does so without comparing either keys or value. Another problem with array_splice() is that it will delete elements after the offset in a sequential manner so you won’t be able to use it to delete non-consecutive elements.

What if you want to delete array elements by their value and those elements are not placed sequentially? The array_diff() function in PHP proves very useful in this situation. This function is used to remove all the elements in the first array that are also present in second array. In other words, you can use this method to remove non-consecutive array elements by their value with ease. You should note that the keys in second array do not matter. Only the values need to match for removal of elements.

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];

$num_diff = ["Amanda", "Megan"];
$asc_diff = ["bogus_a" => "Amanda", "bogus_b" => "Megan"];

$num_array = array_diff($num_array, $num_diff);
print_r($num_array);
/* Output —
Array
(
    [1] => John
    [2] => Kelly
    [4] => Henry
) */

$asc_array = array_diff($asc_array, $asc_diff);
print_r($asc_array);
/* Output —
Array
(
    [second] => John
    [third] => Kelly
    [fifth] => Henry
) */

As you can see in the above example, you can use this technique to remove multiple elements by their value from numeric as well as associative arrays. One thing that I would like to point out is that the keys in the second array don’t have to match the first array for deletion to be successful.

Using array_diff_key() to Delete Multiple Array Elements by Their Keys

Just like you use array_diff() to delete multiple non-consecutive elements from an array by their value, you can use the array_diff_key() function to delete multiple non-consecutive array elements by their key. This function is used to remove all the elements from first array whose key is also present in the second array. You should note the the values in the second array don’t matter. Only the keys need to match for the removal of elements. Here is an example:

PHP

$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$num_array_b = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
              "third" => "Kelly", "fourth" => "Megan",
              "fifth" => "Henry"];

$num_diff = [0, 3];
$num_diff_b = [0 => "Bogus", 3 => "Bogus"];
$asc_diff = ["first" => "Bogus", "third" => "Bogus"];

// $num_diff is used as [0 => 0, 1 => 3];
$num_array = array_diff_key($num_array, $num_diff);
print_r($num_array);
/* Output —
Array
(
    [2] => Kelly
    [3] => Megan
    [4] => Henry
) */

$num_array_b = array_diff_key($num_array_b, $num_diff_b);
print_r($num_array_b);
/* Output —
Array
(
    [1] => John
    [2] => Kelly
    [4] => Henry
) */

$asc_array = array_diff_key($asc_array, $asc_diff);
print_r($asc_array);
/* Output —
Array
(
    [second] => John
    [fourth] => Megan
    [fifth] => Henry
) */

In the above example, the output of the first array_diff_key() operation might come as a surprise to some of you. At first it looks like we are passing the numeric keys of elements to remove like we are supposed to do. However, array_diff_key() turns the simple array we passed into an associative array with the element 0 at index (or key) 0 and the element 3 at index (or key) 1. So the keys that you are passing for removal effectively become 0 and 1 instead of 0 and 3 like you intended to. We remedy the situation by passing bogus values for keys 0 and 3 in the second case and get the expected output. This did not become a problem in the previous section because we were matching keys and not values.

Quick Summary

Let’s recap everything that we have covered in this tutorial.

  1. If you want to remove one element from an array at a time based on its key, you can simply use the unset() function. Just remember that the array won’t be re-indexed after the removal of an element.
  2. Let’s say you want to remove one element from an array at a time based on its value. In this case, you will have to first get the key for that value using the array_search() function. Once you have the key, you can pass it to the unset() function to remove that particular element.
  3. If you want to remove multiple consecutive elements from an array at once, you can use the array_splice() function. One important thing that you need to remember is that you have to pass the offset (not the key or index) to specify the position at which you want to start the removal.
  4. If you want to remove multiple elements from an array but they are not guaranteed to be consecutive, you can use the array_diff() function to remove the elements by their value. Similarly, you can use the array_diff_key() function to remove the elements by their keys.

Let me know if there is anything that you would like me to clarify. Also, you are more than welcome to comment if you know other techniques to delete one or more elements from an array in PHP.

Rate this post —

Very PoorPoorAverageGoodExcellent (5 votes, average: 3.00 out of 5)
Loading...

Tags: |

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0%