I wished to point out that while other comments state that the spread operator should be faster than array_merge, I have actually found the opposite to be true for normal arrays. This is the case in both PHP 7.4 as well as PHP 8.0. The difference should be negligible for most applications, but I wanted to point this out for accuracy.
Below is the code used to test, along with the results:
<?php
$before = microtime(true);
for ($i=0 ; $i<10000000 ; $i++) {
$array1 = ['apple','orange','banana'];
$array2 = ['carrot','lettuce','broccoli'];
$array1 = [...$array1,...$array2];
}
$after = microtime(true);
echo ($after-$before) . " sec for spread\n";
$before = microtime(true);
for ($i=0 ; $i<10000000 ; $i++) {
$array1 = ['apple','orange','banana'];
$array2 = ['carrot','lettuce','broccoli'];
$array1 = array_merge($array1,$array2);
}
$after = microtime(true);
echo ($after-$before) . " sec for array_merge\n";
?>
PHP 7.4:
1.2135608196259 sec for spread
1.1402177810669 sec for array_merge
PHP 8.0:
1.1952061653137 sec for spread
1.099925994873 sec for array_merge