Given an array (One dimensional or multidimensional) and the task is to delete an array element based on key value.
Input: Array (  => 'G'  => 'E'  => 'E'  => 'K'  => 'S' ) Key = 2 Output: Array (  => 'G'  => 'E'  => 'K'  => 'S' )
Using unset() Function: The unset() function is used to remove element from the array. The unset function is used to destroy any other variable and same way use to delete any element of an array. This unset command takes the array key as input and removed that element from the array. After removal the associated key and value does not change.
Parameter: This function accepts single parameter variable. It is required parameter and used to unset the element.
Program 1: Delete an element from one dimensional array.
Array (  => G  => E  => E  => K  => S ) Array (  => G  => E  => K  => S )
Program 2: Delete an element from associative array.
Before delete the element
Array ( [Ankit] => Array ( [C] => 95 [DCO] => 85 ) [Ram] => Array ( [C] => 78 [DCO] => 98 ) [Anoop] => Array ( [C] => 88 [DCO] => 46 ) ) After delete the element
Array ( [Ankit] => Array ( [C] => 95 [DCO] => 85 ) [Anoop] => Array ( [C] => 88 [DCO] => 46 ) )
- PHP | Program to delete an element from array using unset() function
- How to delete last element from a map in C++
- Memory based Vs Register based addressing modes
- How to get the first element of an array in PHP?
- PHP | Second most frequent element in an array
- PHP | Deleting an element from array using array_diff()
- Removing Array Element and Re-Indexing in PHP
- Find the index of an array element in Java
- PHP program to find missing element(s) from an array
- How to remove an array element in a foreach loop?
- Sort a multidimensional array by date element 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.