Remove key and value from an associative array in PHP (2024)

May 9, 2022 ‐3min read

To remove a key and its respective value from an associative array in PHP you can use the unset() function.

&LT?php$mascots = [ 'ElePHPant' => 'php', 'Geeko' => 'openSUSE', 'Gopher' => 'Go'];unset($mascots['Gopher']);print_r($mascots);// Array// (// [ElePHPant] => php// [Geeko] => openSUSE// )

As the name of the function suggests, you use the unset() function to unset a given variable or in this case an array key with its value.

Remove multiple keys from associative array

Removing multiple keys from associative array can be done using the unset() as well. You can pass as many keys to unset as arguments to the unset() function. See the example below where two keys are dropped from the associative array.

&LT?php$mascots = [ 'ElePHPant' => 'php', 'Geeko' => 'openSUSE', 'Gopher' => 'Go'];unset($mascots['Gopher'], $mascots['Geeko']);print_r($mascots);// Array// (// [ElePHPant] => php// )

However useful, the approach above might get somewhat tedious when you need to remove multiple keys from the associative array. In that case there is another option, the array_diff() function. The array_diff() function compares the array you pass it as its first argument and returns an array with the values not present in the array you pass it in the second array.

In contrast to the other options I present here this approach require you to specify the values for which you remove the keys (and values). Instead of keys for which you wish to remove the values (and keys).

&LT?php$mascots = [ 'ElePHPant' => 'php', 'Geeko' => 'openSUSE', 'Gopher' => 'Go'];$values = array_diff($mascots, ['openSUSE', 'Go']);print_r($values);// Array// (// [ElePHPant] => php// )

This last approach seems especially convenient if you need to remove the keys (and values) dynamically in your code.

Remove all keys from associative array

To remove all keys from an associative PHP array is to basically turn the array into a regular numerically indexed array. This can be achieved by grabbing just the values from the associative PHP array.

Since associative arrays in PHP are ordered, just like numerically indexed arrays, we can grab just the values and maintain the original order of the array.

&LT?php$mascots = [ 'ElePHPant' => 'php', 'Geeko' => 'openSUSE', 'Gopher' => 'Go'];$values = array_values($mascots);print_r($values);// Array// (// [0] => php// [1] => openSUSE// [2] => Go// )

The above code sample creates a new array from the values of $mascots and stores the result in the variable $values. The $values array is a regular numerically indexed array, thus all the keys from the associative array $mascots are not present anymore.

As an expert in PHP programming, particularly in array manipulation and handling, I can confidently delve into the concepts and functions mentioned in the provided article regarding associative arrays in PHP.

The article discusses several methods to manipulate associative arrays in PHP, focusing primarily on removing keys and their corresponding values.

  1. unset() Function: The unset() function is used to remove a specific key along with its associated value from an associative array. This is demonstrated in the article by using unset($array['key']).

  2. Removing Multiple Keys: The unset() function can remove multiple keys by passing them as comma-separated arguments: unset($array['key1'], $array['key2']).

  3. array_diff() Function: This function compares arrays and returns the values from the first array that are not present in subsequent arrays. In the context of removing keys from an associative array, array_diff() can be used to create a new array excluding specified values.

  4. array_values() Function: The array_values() function creates a new array containing only the values from the original associative array. This effectively re-indexes the array numerically, removing the original keys but maintaining the order of values.

Understanding these concepts allows efficient manipulation and restructuring of associative arrays in PHP.

Regarding the provided code snippets:

  • The first set of code demonstrates the use of unset() to remove specific keys from the $mascots array.
  • The second set showcases array_diff() to generate a new array without specified values, effectively removing associated keys.
  • The final code block utilizes array_values() to create a new numerically indexed array containing only the values from the $mascots array, thus effectively removing all keys.

In summary, the article elucidates various techniques in PHP to remove keys and values from associative arrays, providing multiple options based on the specific needs of the developer in different scenarios.

Remove key and value from an associative array in PHP (2024)
Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 6312

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.