HH\Lib\Dict\select_keys

Meta Engineer?

This is available as Dict\select_keys in the www repository.

Returns a new dict containing only the keys found in both the input container and the given Traversable

namespace HH\Lib\Dict;

function select_keys<Tk as arraykey, Tv>(
  KeyedContainer<Tk, Tv> $container,
  Traversable<Tk> $keys,
): dict<Tk, Tv>;

The dict will have the same ordering as the $keys Traversable.

Time complexity: O(k), where k is the size of $keys. Space complexity: O(k), where k is the size of $keys.

Parameters

Returns

  • dict<Tk, Tv>

Examples

$dict_with_all_keys = dict["key1" => "value 1", "key2" => "value 2", "key3" => "value 3"];

$present_required_keys = vec["key1"];
$keys_not_present_in_dict = vec["incorrect_keys"];

$dict_with_present_keys = Dict\select_keys($dict_with_all_keys, $present_required_keys);
echo"Result when keys present in dict: \n";
\print_r($dict_with_present_keys);

$dict_with_keys_not_present = Dict\select_keys($dict_with_all_keys, $keys_not_present_in_dict);
echo"Result when keys not present in dict: \n";
\print_r($dict_with_keys_not_present);