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.
<?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.
<?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).
<?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.
<?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.
-
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 usingunset($array['key'])
. -
Removing Multiple Keys: The
unset()
function can remove multiple keys by passing them as comma-separated arguments:unset($array['key1'], $array['key2'])
. -
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. -
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.