In addition to the text and Julian Egelstaffs comment regarding to keep the keys preserved with the + operator:
When they say "input arrays with numeric keys will be renumbered" they MEAN it. If you think you are smart and put your numbered keys into strings, this won't help. Strings which contain an integer will also be renumbered! I fell into this trap while merging two arrays with book ISBNs as keys. So let's have this example:
<?php
$test1['24'] = 'Mary';
$test1['17'] = 'John';
$test2['67'] = 'Phil';
$test2['33'] = 'Brandon';
$result1 = array_merge($test1, $test2);
var_dump($result1);
$result2 = [...$test1, ...$test2]; var_dump($result2);
?>
You will get both:
array(4) {
[0]=>
string(4) "Mary"
[1]=>
string(4) "John"
[2]=>
string(4) "Phil"
[3]=>
string(7) "Brandon"
}
Use the + operator or array_replace, this will preserve - somewhat - the keys:
<?php
$result1 = array_replace($test1, $test2);
var_dump($result1);
$result2 = $test1 + $test2;
var_dump($result2);
?>
You will get both:
array(4) {
[24]=>
string(4) "Mary"
[17]=>
string(4) "John"
[67]=>
string(4) "Phil"
[33]=>
string(7) "Brandon"
}
The keys will keep the same, the order will keep the same, but with a little caveat: The keys will be converted to integers.