|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71658 Change array_merge() to merge valid parameters if any is invalid
Submitted: 2016-02-24 11:24 UTC Modified: 2021-01-06 16:19 UTC
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: wadev at mailbox dot hu Assigned: cmb (profile)
Status: Closed Package: Arrays related
PHP Version: 7.0.3 OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
44 + 4 = ?
Subscribe to this entry?

 [2016-02-24 11:24 UTC] wadev at mailbox dot hu
array_merge() function returns NULL if any of the arguments are not arrays.
This makes for a lot of boilerplate code / guard clauses, such that I, more often than not, rather end up using foreach for merging instead.

I don't know if I am the only one who expects it to either:

- return a merged array containing all of the valid arrays' keys/values, or
- return a merged array containing all of the valid arrays' keys/values before the first "invalid" argument.

This latter might (or might not) be preferrable on performance considerations.

Please propose a change and/or vote on proposal if this is even possible.
If so, I may make a patch if needed.

EDIT: as this seems similar to #64909, another proposal would be the ability to change the emitted E_WARNING into a catchable fatal error.

This is quite strange that in such case we could catch a java.lang.NullPointerException in Java, but nothing in PHP; quite counter-productive.

Test script:
var_dump(array_merge(['what', 'ever'], false));

Expected result:
array(2) {
  string(4) "what"
  string(4) "ever"

Actual result:
<br />
<b>Warning</b>:  array_merge(): Argument #2 is not an array in <b>[...][...]</b> on line <b>2</b><br />


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-04 17:54 UTC]
-Assigned To: +Assigned To: cmb
 [2016-08-04 17:54 UTC]
I don't see why array_merge() should behave differently than
array_intersect(), array_diff() etc. (and I wouldn't change them
all), but if you consider that an improvement, feel free to start
the RFC process (see <>).
 [2016-08-04 23:19 UTC]
-Assigned To: cmb +Assigned To:
 [2021-01-06 16:19 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-01-06 16:19 UTC]
Well, on further consideration this proposal makes no sense to me.
array_merge() excepts arrays, so passing something which is not an
array, and cannot be coerced to one, respectively, is just a
programming error.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Apr 16 15:01:24 2021 UTC