Given an array of elements, we have to delete an element from the array by using the unset() function.
Input : $arr = array("Harsh", "Nishant", "Bikash", "Barun"); unset($arr); Output : Array (  => Harsh  => Nishant  => Bikash ) Input : $arr = array(1, 2, 6, 7, 8, 9); unset($arr); Output : Array (  => 1  => 2  => 6  => 8  => 9 )
unset() function: The function accepts a variable name as parameter and destroy or unset that variable.
Approach: This idea to solve this problem using the unset function is to pass the array key of the respective element which we want to delete from the array as a parameter to this function and thus removes the value associated to it i.e. the element of an array at that index.
Below programs illustrate the above approach:
Array (  => Harsh  => Bikash  => Nishant  => Deep )
Array (  => 1  => 8  => 9  => 7  => 3  => 4 )
Note: The array keys will not be reordered after using the unset() function.
- How to delete an array element based on key in PHP?
- PHP | unset() Function
- PHP | Unset() vs Unlink() Function
- PHP program to find missing element(s) from an array
- How to delete last element from a map in C++
- How to delete last element from a set in C++
- 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
- How to delete text from file using preg_replace() function in PHP ?
- 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
- CSS | unset keyword
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.