HH\Vector::splice

Splice the current Vector in place.

Description

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

Splice the current Vector in place.

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.

Examples

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

<?hh

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

// 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'
Output
object(HH\Vector)#1 (3) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
  [2]=>
  string(6) "yellow"
}
object(HH\Vector)#2 (2) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
}
object(HH\Vector)#3 (1) {
  [0]=>
  string(6) "yellow"
}
object(HH\Vector)#4 (2) {
  [0]=>
  string(3) "red"
  [1]=>
  string(5) "green"
}
object(HH\Vector)#5 (1) {
  [0]=>
  string(6) "yellow"
}