Returns a Map containing the values of the current Map that meet a supplied condition


public function filter(
  (function(Tv):bool) $callback,
): Map<Tk, Tv>;

Only values that meet a certain criteria are affected by a call to filter(), while all values are affected by a call to map().

The keys associated with the current Map remain unchanged in the returned Map.


  • (function(Tv):bool) $callback - The callback containing the condition to apply to the current Map values.

Return Values

  • Map<Tk, Tv> - a Map containing the values after a user-specified condition is applied.


This example shows how filter returns a new Map containing only the values (and their corresponding keys) for which $callback returned true:


namespace Hack\UserDocumentation\API\Examples\Map\Filter;

$m = Map {
  'red' => '#ff0000',
  'green' => '#00ff00',
  'blue' => '#0000ff',
  'yellow' => '#ffff00',
  'purple' => '#663399',

// Filter $m for colors with a 100% red component
$red_100 = $m->filter($hex_code ==> strpos($hex_code, '#ff') === 0);
object(HH\Map)#3 (2) {
  string(7) "#ff0000"
  string(7) "#ffff00"