Horje
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:

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.

Using array_splice() Function

The array_splice() function can be used to remove elements from an array based on their indices. This method is useful if you need to remove elements at specific positions in the array.

Example:

In this example, we’ll remove elements from an array using their indices.

PHP
<?php

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

// Declare indices of elements to be removed
$removeIndices = [1, 3, 5];

// Sort indices in descending order to avoid shifting issues
rsort($removeIndices);

// Remove elements using array_splice
foreach ($removeIndices as $index) {
    if (isset($arr[$index])) {
        array_splice($arr, $index, 1);
    }
}

print_r($arr);

?>

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





Reffered: https://www.geeksforgeeks.org


PHP

Related
How to Compare Two Arrays in PHP? How to Compare Two Arrays in PHP?
Insert a New Element After a Given Element in an Array in PHP Insert a New Element After a Given Element in an Array in PHP
Search an Item in an Array in PHP Search an Item in an Array in PHP
Remove Specific Element from an Array in PHP Remove Specific Element from an Array in PHP
Different Ways to Delete an Item From an Array in PHP Different Ways to Delete an Item From an Array in PHP

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
16