Delete an element from an array in PHP

To delete an element from an array in PHP, you can use any of the PHP built-in functions like unset(), array_splice(), or array_diff()

In this article, we are going to discuss how to remove items from an array in PHP using the above functions.

Using PHP unset() function

The unset() is an in-built function in PHP used to unset/destroy a given variable or an element from an associative array and numeric array. The below example shows how to delete an element from an array.

// unset used in numeric array
$products = ['Phone', 'laptop', 'watch'];
unset($products[1]);
print_r($products);
// Output: Array ( [0] => Phone [2] => watch ) 


// unset used in associative array
$person = ['name'=>'John', 'age'=>25, 'job'=>'Developer'];
unset($person['age']);
print_r($person);
// output: Array ( [name] => John [job] => Developer )

The unset function destroys the given element and it won't return anything.

In the example, can see that after unset, the index of the numeric array is not changed. That means re-indexing is not happening in the unset function. If you want to reindex the keys you can use array_values() after unset like this.

$products = ['Phone', 'laptop', 'watch'];
unset($products[1]);
$products = array_values($products);
print_r($products);
// output: Array ( [0] => Phone [1] => watch ) 

Using PHP array_splice() function

The array_splice() method removes a portion from an array and can be replaced with new elements if needed. Our scenario is to remove an element from an array. So we are passing only the index of the removing portion and length.

$products = ['Phone', 'laptop', 'watch'];
array_splice($products, 1, 1);
print_r($products);
// output: Array ( [0] => Phone [1] => watch )

The array_splice method will re-index the given array after removing the element.

Using PHP array_diff() function

If you want to delete multiple elements from an array in PHP, you can use array_diff() function instead of calling  unset() multiple times. 

$products = ['Phone', 'laptop', 'watch', 'pen', 'books'];
$result = array_diff($products, ['laptop', 'pen']);
print_r($result);
// output: Array ( [0] => Phone [2] => watch [4] => books )

The array_diff() compares an array against another array and returns the values in the first array which are not present in the second array. When using array_diff, The resulting array will not be re-indexed. If you want to re-index, use array_values() after array_diff().

$products = ['Phone', 'laptop', 'watch', 'pen', 'books'];
$result = array_diff($products, ['laptop', 'pen']);
$reindexed = array_values($result);
print_r($reindexed);
// output: Array ( [0] => Phone [1] => watch [2] => books )

Conclusion

To delete an element from an array in PHP, use PHP built-in functions like unset(), array_splice(), or array_diff(). The unset() unset function destroys the given element and the result array will not be reindexed. The array_splice() function removes a portion of the array and the result array will be reindexed. The array_diff() method can be used to remove multiple elements from an array.