PHP: Merging two multi dimensional arrays, overwriting existing values

When merging two multi dimensional arrays in PHP, array_merge_recursive() is the way to go. But is has one flaw:

From the documentation:
“If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.”

This means: If the array key already exists, the value will be changed to an array, containing both values. That might not be the expected behavior for some use cases. Fortunately <daniel (at) danielsmedegaardbuus (dot) dk> and <gabriel (dot) sobrinho (at) gmail (dot) com> wrote a function that behaves the way that the second array value will overwrite the first array value when the key already exists: