Issue #3266568 by mxr576, sourabhjain, quietone: Refactor array_merge() usage in loops as possible for performance