Given an array, we have to delete an element from that array using the array_diff() function in PHP.
Input : $arr = array(2, 8, 9, 7, 6, 5); $arr = array_diff($arr, array(9)); Output : Array (  => 2  => 8  => 7  => 6  => 5 ) Input : $arr = array("shubham", "akshay", "vishal", "sweta"); $arr = array_diff($arr, array("akshay")); Output : Array (  => shubham  => vishal  => sweta )
array_diff() The array_diff() function accepts two or more than two arguments and returns an array containing values from the first array which are not present in other arrays.
Approach: The idea to solve this problem is we will pass two arrays to the array_diff() function as parameters. The first array in the parameter will be the array from which we want to delete the element. The second array will contain a single element which we want to delete from the first array. Finally, we will store the resultant array returned by the array_diff() function in the input array.
Below programs illustrate the above approach:
Array (  => 2  => 8  => 7  => 6  => 5 )
Array (  => Harsh  => Nishant  => Barun  => Bikash )
- Array formed from difference of each element from the largest element in the given array
- Replace each element by the difference of the total size of the array and frequency of that element
- Replace every array element by Bitwise Xor of previous and next element
- Replace every element of the array by its previous element
- Replace every element of the array by its next element
- Deleting all files from a folder using PHP
- Find the smallest after deleting given elements
- Find the largest after deleting the given elements
- Find the k largest numbers after deleting the given elements
- Find the k smallest numbers after deleting given elements
- Find the Largest Cube formed by Deleting minimum Digits from a number
- How to get the first element of an array in PHP?
- PHP | Second most frequent element in an array
- Minimum sum of differences with an element in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.