https://www.php.net/manual/en/function.array-merge.php#refsect1-function.array-merge-examples
If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator:
<?php
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = $array1 + $array2;
var_dump($result);
?>
- How does
array_merge()
(and+
) work under the hood when merging more than two arrays?
According to my understanding it does the merge between the 2, gives a result (a "temporary" array, something like SQL JOIN
s) and then now that resulting one operates with the third one and so on.
- Is
array_merge()
therefore a binary operator?
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
This question is only because I have doubts about how it operates behind the scenes.
When I say "binary operator" I mean something that operates on 2 values