HH\Vector::filterWithKey

Returns a Vector containing the values of the current Vector that meet a supplied condition applied to its keys and values.

Description

public function filterWithKey(
  (function(int,Tv):bool) $callback,
): Vector<Tv>

Returns a Vector containing the values of the current Vector that meet a supplied condition applied to its keys and values.

Only keys and values that meet a certain criteria are affected by a call to filterWithKey(), while all values are affected by a call to mapWithKey().

Parameters

  • (function(int,Tv):bool) $callback - The callback containing the condition to apply to the Vector keys and values.

Return Values

  • Vector<Tv> - a Vector containing the values after a user-specified condition is applied to the keys and values of the current Vector.

Examples

<?hh

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

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

// Only include elements with an odd index
$odd_elements = $v->filterWithKey(
  ($index, $color) ==> ($index % 2) !== 0,
);

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