0

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);
?>
  1. 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 JOINs) and then now that resulting one operates with the third one and so on.

  1. 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

jwa
  • 122
  • 1
  • 7
  • 2
    "Is array_merge() a binary operator?" is wrong question to ask. "How does array_merge() work under the hood when merging more than two arrays?" is much better – Your Common Sense May 13 '23 at 18:41
  • 1
    @KIKOSoftware Here "binary" means "operates on two elements" not "binary" as in encoding or "binary arithmetic". Usually `+` is a "binary operator" by definition, while, say, `!` is unary. – tadman May 13 '23 at 18:41
  • I'm not sure function calls are ever described as operators on the level of `+`. – tadman May 13 '23 at 18:42
  • hi @Your Common Sense right, that's the question, I edited, – jwa May 13 '23 at 18:43
  • Being curious about how PHP works internally is a good thing, but don't forget, it is open-source so you can just look and find out. – tadman May 13 '23 at 18:47
  • 1
    "*Here "binary" means "operates on two elements" not "binary"...*" - @tadman Exactly, that's what I mean in this context – jwa May 13 '23 at 18:47
  • I have a feeling, based on other languages, that `A + B + C` will work differently from `merge(A,B,C)` as in the first case it's forced to combine A+B, then AB+C. In the second case it knows precisely how many elements the final array will have, so it can do it more efficiently. This is, of course, speculation, so a closer analysis of the code, or some performance testing could help illuminate. – tadman May 13 '23 at 18:49
  • @Nigel Ren I have already edited the main question – jwa May 13 '23 at 19:05
  • @OMi Shah No, that question shows examples only between 2 arrays – jwa May 13 '23 at 19:06
  • @tadman So, to understand these 2 features I think we don't need to know what really happens behind the scenes, just read the documentation, right?. The question was because the doc is mentioned as something thought between 2 arrays. – jwa May 13 '23 at 19:09
  • 1
    Fun fact: The documentation can be wrong or out of date. – tadman May 13 '23 at 19:18
  • Sure, I've checked it a few times @tadman – jwa May 13 '23 at 19:47

0 Answers0