How to use array_diff() Function In PHP
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
$arr = array(1, 2, 3, 4, 5);
// Removes the value 3
$arr = array_diff($arr, array(3));
print_r($arr);
?>
Output
Array ( [0] => 1 [1] => 2 [3] => 4 [4] => 5 )
Different Ways to Delete an Item From an Array in PHP
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:
Table of Content
- Using unset() Function
- Using array_splice() Function
- Using array_diff() Function
- Using array_filter() Function
- Using array_search() and unset() Functions
- Using a Loop to Remove Multiple Occurrences