|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #62383 array_replace() arguments not passed by reference
Submitted: 2012-06-21 14:27 UTC Modified: 2012-06-22 21:47 UTC
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: confidential99 at gmail dot com Assigned: salathe (profile)
Status: Closed Package: Arrays related
PHP Version: Irrelevant OS:
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 - 24 = ?
Subscribe to this entry?

 [2012-06-21 14:27 UTC] confidential99 at gmail dot com
The document for the array_replace function says that "array_replace() replaces the values of the first array with the same values from all the following arrays...." The signature for the function indicates the arrays will be passed by reference. 

Test script:

$array1 = array('AMZN'=>223.02,'BA'=>8.14,'SAN'=>6.26,'AAPL'=>586.05);

$array2 = array('AMZN'=>223.84,'BA'=>8.11,'SAN'=>6.40,'FB'=>31.60,'ORCL'=>28.49);

$array3 = array('AMZN'=>224.25,'BA'=>8.10,'ORCL'=>28.39,'INTC'=>27.38);



Expected result:
array(7) { ["AMZN"]=> float(224.25) ["BA"]=> float(8.1) ["SAN"]=> float(6.4) ["AAPL"]=> float(586.05) ["FB"]=> float(31.6) ["ORCL"]=> float(28.39) ["INTC"]=> float(27.38) } 

Actual result:
array(4) { ["AMZN"]=> float(223.02) ["BA"]=> float(8.14) ["SAN"]=> float(6.26) ["AAPL"]=> float(586.05) }


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-22 10:53 UTC]
You have to use

$array1 = array_replace($array1,$array2,$array3);

if you want your expected result. This is not a bug but the expected behaviour as stated in the documentation for array_replace().
 [2012-06-22 19:36 UTC] crmalibu at gmail dot com
Ok, so capturing the return value of the function is the proper way to use this function. However, I agree with the original bug submitter that it seems very odd that the function is documented as accepting arguments by reference, yet it's functionality doesn't *need* args by ref because it will *never* modify any of the args.

I think any reasonable person who reads the php documentation would assume that an argument documented as a reference, has some reason for being a reference. And that reason would be that it may be modified. But this function won't ever modify an arg, and so it shouldn't be documented as such.
 [2012-06-22 21:42 UTC]
Automatic comment from SVN on behalf of salathe
Log: array_replace(_recursive) parameters are not by reference (doc #62383)
 [2012-06-22 21:46 UTC]
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at

 For Windows:
Thank you for the report, and for helping us make PHP better.

The arguments for array_replace() and array_replace_recursive() were incorrectly 
marked as being passed by reference. Thanks for spotting this.
 [2012-06-22 21:46 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: salathe
 [2012-06-22 21:47 UTC]
(Apologies for the wrong generic message above.)

This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Jan 27 06:05:52 2023 UTC