How to Remove an Element from an Array in PHP (7 Examples)

Updated: January 13, 2024 By: Guest Contributor Post a comment

Overview

Handling arrays is a common task in programming, and PHP provides a wide range of built-in functions that make working with them straightforward and efficient. In this tutorial, we’ll explore how to remove an element from an array in PHP using various methods and functions.

Understanding PHP Arrays

Before we dive into removing elements, let’s do a quick refresher on PHP arrays. An array is a data structure that stores one or more similar type of values in a single value container. In PHP, there are three types of arrays:

  • Indexed arrays – Arrays with a numeric index.
  • Associative arrays – Arrays with named keys.
  • Multidimensional arrays – Arrays containing one or more arrays.

Practical Examples

Using unset()

The unset() function is the most straightforward way to remove an element from an array. This statement can be used to unset or remove a single array element or multiple elements. However, it’s important to note that unset() does not re-index the array.


$array = array('apple', 'banana', 'cherry');
unset($array[1]); // Removes 'banana' from the array

After using unset(), the array will have ‘apple’ at index 0 and ‘cherry’ at index 2. The index 1 will no longer exist.

Using array_splice()

For indexed arrays where re-indexing is necessary, array_splice() is used. This function removes elements from an array and optionally replaces them with new elements. It also reindexes the array, which unset() doesn’t do.


$array = array('apple', 'banana', 'cherry');
array_splice($array, 1, 1); // Removes 'banana' and re-indexes

The output now would be ‘apple’ at index 0 and ‘cherry’ at index 1.

Using array_diff()

When you need to remove a value without knowing its index, array_diff() can be used. This function compares the values of two arrays and returns the differences. It’s useful for removing a specific value completely from the array.


$array = array('apple', 'banana', 'cherry');
$array = array_diff($array, array('banana'));

The resulting array will have only ‘apple’ and ‘cherry’, and it will be re-indexed.

For Associative Arrays

Working with associative arrays is similar to indexed arrays when removing elements. The unset() function is still the best tool for the job.


$array = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry');
unset($array['b']); // Removes the element with key 'b'

After using unset(), the array will only contain keys ‘a’ and ‘c’.

Use of array_filter()

The array_filter() function can also be used to remove elements from an array. Although it is generally used to filter elements based on certain conditions, you can indirectly remove elements by filtering out the elements you don’t want.


$array = array('apple', 'banana', 'cherry', 'banana');
$array = array_filter($array, function($item) {
    return $item !== 'banana';
});

This will remove all occurrences of ‘banana’ from the array.

Using array_values()

After removing elements from an array using the unset() function, if you need the array to be re-indexed use array_values(). This function returns all the values from the array and indexes the array numerically.


$array = array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry');
unset($array['b']);
$array = array_values($array);

The new array will contain ‘apple’ at index 0 and ‘cherry’ at index 1.

Removing Multiple Elements

Use any of the aforementioned techniques in combination with a loop to remove multiple elements. For example:


$array = array('apple', 'banana', 'cherry', 'date', 'elderberry');
$elementsToRemove = array('banana', 'date');
foreach ($elementsToRemove as $element) {
    $key = array_search($element, $array);
    if ($key !== false) {
        unset($array[$key]);
    }
}
$array = array_values($array); // Re-index the array

After the loop runs, ‘banana’ and ‘date’ will be removed and the array will be re-indexed.

Conclusion

In this tutorial, we’ve gone over different techniques to remove elements from arrays in PHP. Remember that each function works best in different scenarios. unset() is great for a quick removal by key, array_splice() for re-indexing after removal, array_diff() for value-based removal, and array_filter() for conditional removal. Combining these with loops and other array functions you can manipulate arrays in any way you require for your application.

Understanding how to work efficiently with arrays is crucial for PHP developers since arrays are one of the most commonly used data structures. Practice these techniques to enhance your knowledge and coding efficiency in PHP.