How to Remove Multiple Elements from an Array in PHP?
Last Updated :
31 Jul, 2024
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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[0] => 1
[1] => 3
[2] => 5
)
Similar Reads
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read
JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po
3 min read
How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i
4 min read
How to Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function
5 min read
Removing Array Element and Re-Indexing in PHP In order to remove an element from an array, we can use unset() function which removes the element from an array, and then use array_values() function which indexes the array numerically automatically. Function Usedunset(): This function unsets a given variable. Syntax:void unset ( mixed $var [, mix
2 min read