HH\Vector::set

Stores a value into the current Vector with the specified key, overwriting the previous value associated with the key

public function set(
  int $key,
  Tv $value,
): Vector<Tv>;

If the key is not present, an exception is thrown. If you want to add a value even if the key is not present, use add().

$vec->set($k,$v) is semantically equivalent to $vec[$k] = $v (except that set() returns the current Vector).

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

Parameters

  • int $key
  • Tv $value

Returns

Examples

Since Vector::set() returns a shallow copy of $v itself, you can chain a bunch of set() calls together.

$v = Vector {'red', 'green', 'blue', 'yellow'};

// Set the first element to 'RED'
$v->set(0, 'RED');

\var_dump($v);

// Set the second and third elements using chaining
$v->set(1, 'GREEN')
  ->set(2, 'BLUE');

\var_dump($v);