array_merge vs array_replace vs union

Attention: Les informations de ce billet sont susceptibles d'être obsolètes car vieux de plus 2 ans.

Warning: The information you are reading may be obsolete, this post was published more than 2 years ago.

This is just a reminder for me because I always struggle to find a good documentation:

When dealing with simple arrays without numeric indexes in PHP, there are 3 importants things to keep in mind:

  1. The arguments order passed to the function or used with the + sign is important;
  2. array_merge and array_replace are interchangeable since their results are identical;
  3. the + sign gives the same results as array_merge and array_replace but the arguments order must be reverse;

So performance wise, the + sign is better but beware or your arguments orders!!

And here’s a little demo to make it clear to you

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.