Different Ways to Delete an Item From an Array in PHP
Last Updated :
23 May, 2024
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:
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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[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);
?>
OutputArray
(
[0] => 1
[1] => 2
[3] => 4
[5] => 5
)
Similar Reads
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
PHP | Deleting an element from array using array_diff() Given an array, we have to delete an element from that array using the array_diff() function in PHP.Examples:Input : $arr = array(2, 8, 9, 7, 6, 5); $arr = array_diff($arr, array(9)); Output : Array ( [0] => 2 [1] => 8 [3] => 7 [4] => 6 [5] => 5 ) Input : $arr = array("shubham", "aksh
2 min read
How to delete an element from an array using JavaScript ? The 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. In this article, we will discuss different ways to remove elements from the array.
5 min read
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
PHP | Program to delete an element from array using unset() function Given an array of elements, we have to delete an element from the array by using the unset() function. Examples: Input : $arr = array("Harsh", "Nishant", "Bikash", "Barun"); unset($arr[3]); Output : Array ( [0] => Harsh [1] => Nishant [2] => Bikash ) Input : $arr = array(1, 2, 6, 7, 8, 9);
2 min read