HH\Map::removeKey

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

Description

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

This method is interchangeable with remove().

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

Parameters

  • $k - The key to remove.

Return Values

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"
}