How to unset array key in php?
View Discussion Show
Improve Article Save Article View Discussion Improve Article Save Article Given an array (One dimensional or multidimensional) and the task is to delete an array element based on key value. Examples: Input: Array ( [0] => 'G' [1] => 'E' [2] => 'E' [3] => 'K' [4] => 'S' ) Key = 2 Output: Array ( [0] => 'G' [1] => 'E' [3] => 'K' [4] => 'S' ) Recommended: Please try your approach on {IDE} first, before moving on to the solution.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. Syntax: unset($variable) 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.
Output: Array ( [0] => G [1] => E [2] => E [3] => K [4] => S ) Array ( [0] => G [1] => E [3] => K [4] => S ) Program 2: Delete an element from associative array.
Output: Before delete the element PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. There are different ways to delete an array element, where some are more useful for some specific tasks than others. Deleting a single array elementIf you want to delete just one array
element you can use If you know the value and don’t know the key to delete the element you can use
|