HH\Lib\Dict\merge

Meta Engineer?

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

Merges multiple KeyedTraversables into a new dict

namespace HH\Lib\Dict;

function merge<Tk as arraykey, Tv>(
  KeyedTraversable<Tk, Tv> $first,
  KeyedContainer<Tk, Tv> ...$rest,
): dict<Tk, Tv>;

In the case of duplicate keys, later values will overwrite the previous ones.

Time complexity: O(n + m), where n is the size of $first and m is the combined size of all the ...$rest Space complexity: O(n + m), where n is the size of $first and m is the combined size of all the ...$rest

Parameters

Returns

  • dict<Tk, Tv>

Examples

$result = Dict\merge(dict[1 => 2, 2 => 4], dict[7 => 2, 100 => 4]);
print_r($result);
//result: dict[1=>2, 2=>4, 7=>2, 100=>4]

$result = Dict\merge(dict[1 => 2, 2 => 4], dict[7 => 2, 2 => 100]);
print_r($result);
//result: dict[1=>2, 2=>100, 7=>2]