How to Delete an Element from an Array in PHP

Arrays are a fundamental data structure in PHP, and being able to manipulate them is an essential skill for any PHP developer. One common task is deleting elements from an array, whether it's to remove unwanted data or reorganize the array.

In this article, we'll explore several methods to delete elements from an array in PHP.

Method 1: Using unset()

The unset() function in PHP is a straightforward way to remove an element from an array. You specify the array and the element's key or index, and unset() takes care of the removal.

Here's an example of how to use unset() to delete an element from an array:

$fruits = ["apple", "banana", "cherry"];

// Remove the element at index 1 (banana)
unset($fruits[1]);

print_r($fruits);

Output:

Array
(
    [0] => apple
    [2] => cherry
)

In this example, we use unset($fruits[1]) to remove the element at index 1 (which contains "banana") from the $fruits array. The remaining elements are reindexed, leaving no gap.


Method 2: Using array_splice()

The array_splice() function provides a flexible way to remove elements from an array by specifying the start position and the number of elements to remove.

Here's an example:

$fruits = ["apple", "banana", "cherry"];

// Remove one element starting from index 1 (banana)
array_splice($fruits, 1, 1);

print_r($fruits);

Output:

Array
(
    [0] => apple
    [1] => cherry
)

In this example, we remove one element starting from index 1 (which contains "banana") from the $fruits array. Like unset(), array_splice() also reindexes the array.


Method 3: Using array_filter()

The array_filter() function allows you to filter an array based on a callback function. You can use this function to exclude elements from an array based on specific criteria.

Here's an example of how to use array_filter() to delete elements from an array:

$fruits = ["apple", "banana", "cherry"];

// Remove elements containing the letter 'a'
$fruits = array_filter($fruits, function($fruit) {
    return stripos($fruit, 'a') === false;
});

print_r($fruits);

Output:

Array
(
    [1] => banana
)
  1. In this example, we use array_filter() with a callback function that checks if the element contains the letter 'a'.
  2. Any element meeting the condition is removed from the array.

Method 4: Using array_values() (Reindexing)

After deleting elements using methods like unset() or array_splice(), the array may have gaps in the indices.

To reindex the array and remove these gaps, you can use the array_values() function.

Here's an example:

$fruits = ["apple", "banana", "cherry"];

// Remove the element at index 1 (banana)
unset($fruits[1]);

// Reindex the array
$fruits = array_values($fruits);

print_r($fruits);

Output:

Array
(
    [0] => apple
    [1] => cherry
)
  1. In this example, we first use unset() to remove the element at index 1 (which contains "banana").
  2. Then, we reindex the array using array_values(). This ensures that the remaining elements are reindexed sequentially.

Conclusion

Deleting elements from an array in PHP is a common and essential operation. Depending on your needs and coding style, you can choose the most suitable method. Whether it's using unset() for direct removal, array_splice() for more precise control, array_filter() for conditional removal, or array_values() for reindexing, these methods provide the flexibility to manipulate arrays effectively in your PHP applications.