HH\Vector::addAll

For every element in the provided Traversable, append a value into this Vector, assigning the next available integer key for each.

Description

public function addAll(
  ?Traversable<Tv> $it,
): Vector<Tv>

For every element in the provided Traversable, append a value into this Vector, assigning the next available integer key for each.

If you want to overwrite the values for existing keys, use setAll().

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

Parameters

  • ?Traversable<Tv> $it

Return Values

Examples

The following example adds a collection of values to the Vector $v and also adds multiple collections of values to $v through chaining. Since Vector::addAll() returns a shallow copy of $v itself, you can chain a bunch of addAll() calls together, and that will add all those collection of values to $v.

<?hh

namespace Hack\UserDocumentation\API\Examples\Vector\AddAll;

$v = Vector {};

// Add all the values in a Set
$v->addAll(Set {'a', 'b'});

// Add all the values in a Vector
$v->addAll(Vector {'c', 'd'});

// Add all the values in a Map
$v->addAll(Map {'foo' => 'e', 'bar' => 'f'});

// Add all the values in an array
$v->addAll(array('g', 'h'));

// Vector::addAll returns the Vector so it can be chained
$v->addAll(ImmSet {'i', 'j'})
  ->addAll(ImmVector {'k', 'l'});

var_dump($v);
Output
object(HH\Vector)#1 (12) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
  [5]=>
  string(1) "f"
  [6]=>
  string(1) "g"
  [7]=>
  string(1) "h"
  [8]=>
  string(1) "i"
  [9]=>
  string(1) "j"
  [10]=>
  string(1) "k"
  [11]=>
  string(1) "l"
}