php.net |  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
Votes:2
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
Status: Closed Package: Arrays related
PHP Version: Irrelevant OS:
Private report: No CVE-ID:
 [2012-06-21 14:27 UTC] confidential99 at gmail dot com
Description:
------------
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:
---------------
<?php

$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);

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

var_dump($array1);



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) }

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-22 10:53 UTC] fd@php.net
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] salathe@php.net
Automatic comment from SVN on behalf of salathe
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=326300
Log: array_replace(_recursive) parameters are not by reference (doc #62383)
 [2012-06-22 21:46 UTC] salathe@php.net
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
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
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] salathe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: salathe
 [2012-06-22 21:47 UTC] salathe@php.net
(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-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 05:03:21 2014 UTC