If you wrote some PHP, most likely you have used array_merge here and there for your arrays. You may have met array_replace function, introduced in PHP5.3. Finally, you could notice + operator (aka union).

Since all 3 do similar thing, and the docs don’t quite describe the difference between them, here’s a nice image of it


Screenshot from 2015-11-13 00:34:21



There are a few things to consider:

  1. array_merge and array_replace work just the same for keyed (associative) elements, so they can be used interchangeably:

  2. array_replace and + do the opposite always:
  3. array_merge behaves differently to the other 2 with numeric arrays:



That being said, we could think of the + operator as kinda redundant, since the same can be achieved with array_replace function.

However, there are cases, where it comes in handy: say you have an $options array being passed to a function/method, and there are also defaults to be used as fallback:



It's only fair to share...Tweet about this on TwitterShare on RedditPin on PinterestShare on FacebookShare on Google+

Related Post

New project open – RMA@SOFA Starting new project called RMA@SOFA. It will cover most of the needs of service points, starting with IT services, then much more! Roadmap and ...
Laravel Searchable – the best package for el... In this first post of a series about - the package that allows working with eloquent models in even simp...
Laravel 5 Eloquent Global Scope how-to If you are looking for an example of Eloquent Global Scope feature, then you're in the right place! Check the ready-to-use demo at ... h...