HH\Vector::splice

Splice the current Vector in place

public function splice(
  int $offset,
  ?int $len = NULL,
): void;

This function provides the functionality of array_splice() for Vectors (except that splice() does not permit specifying replacement values. If a third ("replacement values") parameter is specified, an exception is thrown.

Note that this function modifies the current Vector in place.

Parameters

  • int $offset - The (0-based) key at which to begin the splice. If negative, then it starts that far from the end of the current Vector.
  • ?int $len = NULL - The length of the splice. If null, then the current Vector is spliced until its end.

Returns

  • void

Examples

The following example shows how to use $offset and $len together:

// Remove the element at index 2:
$v = Vector {'red', 'green', 'blue', 'yellow'};
$v->splice(2, 1);
\var_dump($v); // $v contains 'red', 'green', 'yellow'

// Remove elements starting at index 2:
$v = Vector {'red', 'green', 'blue', 'yellow'};
$v->splice(2);
\var_dump($v); // $v contains 'red', 'green'

// Remove three elements starting at index 0:
$v = Vector {'red', 'green', 'blue', 'yellow'};
$v->splice(0, 3);
\var_dump($v); // $v contains 'yellow'

// Remove elements starting two positions from the end:
$v = Vector {'red', 'green', 'blue', 'yellow'};
$v->splice(-2);
\var_dump($v); // $v contains 'red', 'green

// Remove elements starting at index 0 and stopping one position from the end:
$v = Vector {'red', 'green', 'blue', 'yellow'};
$v->splice(0, -1);
\var_dump($v); // $v contains 'yellow'