HH\Map::addAll

For every element in the provided Traversable, add a key/value pair into the current Map

public function addAll(
  ?Traversable<Pair<Tk, Tv>> $iterable,
): Map<Tk, Tv>;

This method is equivalent to Map::setAll(). If a key in the Traversable exists in the Map, then the value associated with that key in the Map is overwritten.

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

Parameters

  • ?Traversable<Pair<Tk, Tv>> $iterable

Returns

Examples

The following example adds a collection of key-value pairs to the Map $m and also adds multiple collections of key-value pairs to $m through chaining. Since Map::addAll() returns a shallow copy of $m itself, you can chain a bunch of addAll() calls together.

$m = Map {};

// Add all the key-value pairs in an array
$m->addAll(array(Pair {'red', '#ff0000'}));

// Map::addAll returns the Map so it can be chained
$m->addAll(Vector {
  Pair {'green', '#00ff00'},
  Pair {'blue', '#0000ff'},
})
  ->addall(ImmVector {
    Pair {'yellow', '#ffff00'},
    Pair {'purple', '#663399'},
  });

var_dump($m);
Output
object(HH\Map)#1 (5) {
  ["red"]=>
  string(7) "#ff0000"
  ["green"]=>
  string(7) "#00ff00"
  ["blue"]=>
  string(7) "#0000ff"
  ["yellow"]=>
  string(7) "#ffff00"
  ["purple"]=>
  string(7) "#663399"
}