HH\Set::fromKeysOf

Creates a Set from the keys of the specified container

Description

public static function fromKeysOf<Tk, Tv2>(
  ?KeyedContainer<Tk,Tv2> $container,
): Set<Tk>

The keys of the container will be the values of the Set.

Parameters

  • $container - The container with the keys used to create the Set.

Return Values

  • Set<Tk> - A Set built from the keys of the specified container.

Examples

This example creates new Sets from a string-keyed Map and associative array:

<?hh

namespace Hack\UserDocumentation\API\Examples\Set\FromKeysOf\Strings;

$fruit_calories = Map {
  'apple' => 95,
  'orange' => 45,
};

$vegetable_calories = array(
  'cabbage' => 176,
  'potato' => 163,
);

// Create a Set from the keys of a Map
$fruit_names = Set::fromKeysOf($fruit_calories);
var_dump($fruit_names);

// Create a Set from the keys of an associative array
$vegetable_names = Set::fromKeysOf($vegetable_calories);
var_dump($vegetable_names);
Output
object(HH\Set)#2 (2) {
  string(5) "apple"
  string(6) "orange"
}
object(HH\Set)#3 (2) {
  string(7) "cabbage"
  string(6) "potato"
}

This example creates new Sets from an int-keyed Map and associative array:

<?hh

namespace Hack\UserDocumentation\API\Examples\Set\FromKeysOf\Ints;

$uploaders_by_id = Map {
  4993063 => 'Amy Smith',
  9361760 => 'John Doe',
};

$commenters_by_id = array(
  7424854 => 'Jane Roe',
  5740542 => 'Joe Bloggs',
);

// Create a Set from the integer keys of a Map
$uploader_ids = Set::fromKeysOf($uploaders_by_id);
var_dump($uploader_ids); // $uploader_ids contains 4993063, 9361760

// Create a Set from the integer keys of an associative array
$commenter_ids = Set::fromKeysOf($commenters_by_id);
var_dump($commenter_ids); // $commenter_ids contains 7424854, 5740542
Output
object(HH\Set)#2 (2) {
  int(4993063)
  int(9361760)
}
object(HH\Set)#3 (2) {
  int(7424854)
  int(5740542)
}