Вопрос

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?

Это было полезно?

Решение

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>

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top