How to Delete an Array Element by Value in PHP

In PHP, arrays are a versatile and commonly used data structure. You may often need to remove specific elements from an array by their values.

In this article, we will explore various methods for deleting array elements by their values in PHP.

The array_search() Function

The array_search() function in PHP allows you to search for a value within an array and retrieve its corresponding key if it exists.

If the value is found, array_search() returns the key; otherwise, it returns false.

Here's the basic syntax of array_search():

array_search($value, $array)

1. Using array_search() with unset() to Delete by Value

To delete an array element by its value, you can follow these steps:

  1. Use array_search() to find the key of the element you want to delete.
  2. Check if array_search() returns a valid key (i.e., it's not false).
  3. If a valid key is found, use unset() to remove the element associated with that key from the array.

Let's see an example:

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

$key = array_search($valueToRemove, $fruits);

if ($key !== false) {
    unset($fruits[$key]);
}

print_r($fruits);

Output:

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

In this example:

  1. We use array_search($valueToRemove, $fruits) to find the key associated with the value "banana" in the $fruits array.

  2. We check if array_search() returns a valid key (not false). If it does, it means the value was found in the array.

  3. Inside the if block, we use unset($fruits[$key]) to delete the element from the $fruits array. The key $key corresponds to the position of "banana" in the array.

As a result, the element with the specified value ("banana") is removed from the array, and the array is re-indexed.


2. Using array_filter() method (get a new Array)

The array_filter() function in PHP 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, including their values, and it will not affect the original array, it returns a new array.

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

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

$valueToRemove = "banana";

$newFruits = array_filter($fruits, function ($fruit) use ($valueToRemove) {
    return $fruit !== $valueToRemove;
});

print_r($fruits);
print_r($newFruits);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => date
)

Array
(
    [0] => apple
    [2] => cherry
    [3] => date
)
  1. In this example, we use array_filter() with a callback function that checks if each element in the array is not equal to the value we want to remove ("banana" in this case).
  2. Elements that meet the condition are retained, while the element with the specified value is excluded from the resulting array.

Conclusion

Performing array delete by value using array_search() and unset() is a useful technique in PHP. It allows you to efficiently remove specific elements from an array based on their values, making it a valuable tool in array manipulation tasks within your PHP applications.