Given an associative array containing array elements and the task is to remove a key and its value from the associative array.
Input : array( "name" => "Anand", "roll"=> "1") Output : Array ( [roll] => 1 ) Input : array( "1" => "Add", "2" => "Multiply", "3" => "Divide") Output : Array (  => Multiply  => Divide )
Method 1: Using unset() function: The unset() function is used to unset a key and its value in an associative array.
void unset( $array_name['key_to_be_removed'] )
Array (  => Multiply  => Divide )
Method 2: Using array_diff_key() function: This function is used to get the difference between one or more arrays. This function compares the keys between one or more arrays and returns the difference between them.
array array_diff_key( $array_name, array_flip((array) ['keys_to_be_removed'] )
Array (  => b  => c )
- How to loop through an associative array and get the key in PHP?
- Multidimensional Associative Array in PHP
- Convert an object to associative array in PHP
- How to check an array is associative or sequential in PHP?
- How to get numeric index of associative array in PHP?
- PHP program to add item at the beginning of associative array
- Iterate associative array using foreach loop in PHP
- How to access an associative array by integer index in PHP?
- Associative Arrays in PHP
- PHP | Remove duplicate elements from 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.