HH\Map::filter

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.

Guide

Parameters

  • (function(Tv): bool) $callback

Returns

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

Examples

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

$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);
\var_dump($red_100);