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.
- PHP | unset() Function
- PHP | Unset() vs Unlink() Function
- PHP | Unset Session Variable
- How to set and unset cookies using jQuery?
- How to delete an array element based on key in PHP?
- Difference between revert and unset keyword in CSS
- CSS | unset keyword
- CSS Value | Unset
- Lodash _.unset() Method
- How to delete text from file using preg_replace() function in PHP ?
- How to delete a file using PHP ?
- How to delete last element from a set in C++
- How to delete last element from a map in C++
- How to delete an item or object from the array using ng-click ?
- PHP | MySQL Delete Query
- How to recursively delete a directory and its entire contents (files + sub dirs) in PHP?
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.