HH\Map::removeKey

Removes the specified key (and associated value) from the current Map.

Description

public function removeKey(
  Tk $k,
): Map<Tk,Tv>

Removes the specified key (and associated value) from the current Map.

This method is interchangeable with remove().

Future changes made to the current Map ARE reflected in the returned Map, and vice-versa.

Parameters

  • Tk $k - The key to remove.

Return Values

  • Map<Tk,Tv> - A shallow copy of the current Map with the key removed; the current Map is also updated with the key removed.

Examples

Since Map::removeKey() returns a shallow copy of $m itself, you can chain a bunch of removeKey() calls together.

<?hh

namespace Hack\UserDocumentation\API\Examples\Map\RemoveKey;

$m = Map {
  'red' => '#ff0000',
  'green' => '#00ff00',
  'blue' => '#0000ff',
  'yellow' => '#ffff00',
};

// Remove key 'red'
$m->removeKey('red');
var_dump($m);

// Remove keys 'green' and 'blue'
$m->removeKey('green')->removeKey('blue');
var_dump($m);
Output
object(HH\Map)#1 (3) {
  ["green"]=>
  string(7) "#00ff00"
  ["blue"]=>
  string(7) "#0000ff"
  ["yellow"]=>
  string(7) "#ffff00"
}
object(HH\Map)#1 (1) {
  ["yellow"]=>
  string(7) "#ffff00"
}