Doc #62383 [Csd]: array_replace() arguments not passed by reference
| From: | salathe@php.net | Date: | Fri, 22 Jun 2012 21:47:58 +0000 |
| Subject: | Doc #62383 [Csd]: array_replace() arguments not passed by reference | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8499@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62383&edit=1
ID: 62383
Updated by: salathe@php.net
Reported by: confidential99 at gmail dot com
Summary: array_replace() arguments not passed by reference
Status: Closed
Type: Documentation Problem
Package: Arrays related
PHP Version: Irrelevant
Assigned To: salathe
Block user comment: N
Private report: N
New Comment:
(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.
Previous Comments:
------------------------------------------------------------------------
[2012-06-22 21:46:31] 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:42:04] salathe@php.net
Automatic comment from SVN on behalf of salathe
Revision: http://svn.php.net/viewvc/?view=revision&revision=326300
Log: array_replace(_recursive) parameters are not by reference (doc #62383)
------------------------------------------------------------------------
[2012-06-22 19:36:09] 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 10:53:08] 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-21 14:27:17] 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) }
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62383&edit=1