How to Remove Multiple Elements from an Array in PHP?

Given an array containing some elements, the task is to remove some elements from the array in PHP.

Below are the approaches to remove multiple elements from an array in PHP:

Table of Content

  • Using array_diff() Function
  • Using array_filter() Function
  • Using a Loop and unset() Function
  • Using array_diff_key() Function

Using array_diff() Function

The array_diff() function compares two or more arrays and returns the values in the first array that are not present in the other arrays. This is useful for removing specific elements from an array.

Example: This example shows the use of the above-explained approach.

PHP
<?php

// Declare an Array
$arr = [1, 2, 3, 4, 5, 6];

// Declare an array containing 
// elements that need to remove
$remove = [2, 4, 6];

// Remove elements
$result = array_diff($arr, $remove);

print_r($result);

?>

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

Explanation:

  • $arr is the original array.
  • $remove contains the elements to be removed.
  • array_diff($arr, $remove) returns the values from $array that are not present in $remove.

Using array_filter() Function

The array_filter() function filters the elements of an array using a callback function. This method allows for custom logic to determine which elements to remove.

Example: This example shows the use of the above-explained approach.

PHP
<?php

// Declare an Array
$arr = [1, 2, 3, 4, 5, 6];

// Declare an array containing 
// elements that need to remove
$remove = [2, 4, 6];

// Remove elements using array_filter
$result = array_filter($arr, 
    function($value) use ($remove) {
        return !in_array($value, $remove);
    });

print_r($result);

?>

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

Explanation:

  • $arr is the original array.
  • $remove contains the elements to be removed.
  • array_filter($arr, function($value) use ($remove) { return !in_array($value, $remove); }) filters out the elements present in $remove.

Using a Loop and unset() Function

You can use a foreach loop to iterate over the elements to be removed and use unset() to remove them from the original array.

Example: This example shows the use of the above-explained approach.

PHP
<?php

// Declare an Array
$arr = [1, 2, 3, 4, 5, 6];

// Declare an array containing 
// elements that need to remove
$remove = [2, 4, 6];

// Remove elements using unset
foreach ($remove as $value) {
    if (($key = array_search($value, $arr)) !== false) {
        unset($arr[$key]);
    }
}

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

print_r($arr);

?>

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

Explanation:

  • $arr is the original array.
  • $remove contains the elements to be removed.
  • The foreach loop iterates over $remove, and for each element, array_search($value, $arr) finds the key, and unset($arr[$key]) removes the element.
  • array_values($arr) reindexes the array to have sequential keys.

Using array_diff_key() Function

The array_diff_key() function compares the keys of two arrays and returns the values in the first array whose keys are not present in the second array. This method is useful when you know the keys of the elements to be removed.

Example: This example shows the use of the above-explained approach.

PHP
<?php

// Declare an Array
$arr = [1, 2, 3, 4, 5, 6];

// Declare an array containing 
// elements that need to remove
$removeKey = [1, 3, 5];

// Remove elements using array_diff_key
$result = array_diff_key($arr, array_flip($removeKey));

print_r($result);

?>

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

Explanation:

  • $array is the original array.
  • $removeKeys contains the keys of the elements to be removed.
  • array_flip($removeKeys) creates an array where the keys are the values from $removeKeys.
  • array_diff_key($array, array_flip($removeKeys)) removes the elements from $array whose keys are present in $removeKeys.


Contact Us