HH\Vector::slice

Returns a subset of the current Vector starting from a given key up to, but not including, the element at the provided length from the starting key.

Description

public function slice(
  int $start,
  int $len,
): Vector<Tv>

Returns a subset of the current Vector starting from a given key up to, but not including, the element at the provided length from the starting key.

$start is 0-based. $len is 1-based. So slice(0, 2) would return the elements at key 0 and 1.

The returned Vector will always be a proper subset of this Vector.

Parameters

  • int $start - The starting key of this Vector to begin the returned Vector.
  • int $len - The length of the returned Vector.

Return Values

  • Vector<Tv> - A Vector that is a proper subset of the current Vector starting at $start up to but not including the element $start + $len.

Examples

<?hh

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

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

// Start at index 1 ('green') and include 3 elements
$v2 = $v->slice(1, 3);

var_dump($v2);
Output
object(HH\Vector)#2 (3) {
  [0]=>
  string(5) "green"
  [1]=>
  string(4) "blue"
  [2]=>
  string(6) "yellow"
}