Different Ways to Delete an Item From an Array in PHP

Given an array containing some elements, the task is to Delete an item from an array using PHP.

Below are the approaches to delete an item from an Array in PHP:

Table of Content

  • Using unset() Function
  • Using array_splice() Function
  • Using array_diff() Function
  • Using array_filter() Function
  • Using array_search() and unset() Functions
  • Using a Loop to Remove Multiple Occurrences

Using unset() Function

The unset() function is used to remove an element from an array by its key. This function is basic and easy to use.

Example: The unset() function removes the element at index 2 and the keys are not reindexed, which might be important in some use cases.

PHP
<?php
$arr = array(1, 2, 3, 4, 5);
// Removes the element at index 2
unset($arr[2]);
print_r($arr);
?>

Output
Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [4] => 5
)

Using array_splice() Function

The array_splice() function removes a portion of the array and can reindex the array elements.

Example: The array_splice() function removes 1 element starting at index 2 and reindexes the array.

PHP
<?php
$arr = array(1, 2, 3, 4, 5);
// Remove 1 element starting at index 2
array_splice($arr, 2, 1);
print_r($arr);
?>

Output
Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
)

Using array_diff() Function

The array_diff() function compares arrays and returns the differences. This can be used to remove specific values.

Example: The array_diff() function compares the original array with an array containing the value to be removed and returns an array with the differences.

PHP
<?php
$arr = array(1, 2, 3, 4, 5);
// Removes the value 3
$arr = array_diff($arr, array(3));
print_r($arr);
?>

Output
Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [4] => 5
)

Using array_filter() Function

The array_filter() function filters elements of an array using a callback function.

Example: The array_filter() function uses a callback to filter out elements that do not match the condition.

PHP
<?php
$arr = array(1, 2, 3, 4, 5);
// Remove the element with value 3
$arr = array_filter($arr, function($value) {
    return $value != 3;
});
print_r($arr);
?>

Output
Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [4] => 5
)

Using array_search() and unset() Functions

You can combine array_search() to find the index of the value and unset() to remove it.

Example: The array_search() function finds the index of the value to be removed, and unset() removes the element at that index.

PHP
<?php

$arr = array(1, 2, 3, 4, 5);
// Find the index of the value 3
$key = array_search(3, $arr);
if ($key !== false) {
    // Remove the element at 
    // the given index (key)
    unset($arr[$key]);
}
print_r($arr);
?>

Output
Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [4] => 5
)

Using a Loop to Remove Multiple Occurrences

If you need to remove all occurrences of a value, you can loop through the array.

Example: The loop iterates through the array, and unset() removes elements that match the specified value.

PHP
<?php
$arr = array(1, 2, 3, 4, 3, 5);
$remVal = 3;
foreach ($arr as $key => $value) {
    if ($value == $remVal) {
        // Remove the element
        unset($arr[$key]);
    }}
print_r($arr);
?>

Output
Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [5] => 5
)