Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP:What is faster? array_merge or array union operator (+)?

I have two arrays with unique numeric keys. How can I merge them faster?

like image 221
Hovsep Avatar asked Oct 15 '25 03:10

Hovsep


1 Answers

10000000 iterations on PHP 7.1.5 with two small multi-dimensional arrays:

  • plus-operator (+): 1.966 seconds
  • array_merge: 9.431 seconds

So the plus operator is faster but if you have collisions, the result is different.

like image 121
Jack Avatar answered Oct 17 '25 19:10

Jack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!