Question

Given the following code

<?hh
$a = Vector {"qux"};
$b = Vector {"foo","bar"};

$a = array_merge($a, $b);

The following error is raised:

Warning: Invalid operand type was used: array_merge expects array(s)

Is there any workaround for array_merge for vectors and maps, without making a foreach by hand, of the vector?

Était-ce utile?

La solution

Answering to my self.

In fact array_merge is not listed under the list of functions with support for hack Collections.

The best way to merge Vectors is the function addAll.

<?hh
$a = Vector {"qux"};
$b = Vector {"foo","bar"};

$a->addAll($b);

var_dump($a);

In the case of Map, it's a bit more complicated and a combination of foreach and addAll is required to set the Pair <Tk,Tv>

Autres conseils

Well I wanted to merge a map, and found out you could also use the setAll() method.

$this->params->setAll($parent->params);

The params of the $parent overwrite the keys of $this->params

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top