Doc #62383 [Com]: array_replace() arguments not passed by reference
| From: | crmalibu at gmail dot com | Date: | Fri, 22 Jun 2012 19:36:09 +0000 |
| Subject: | Doc #62383 [Com]: array_replace() arguments not passed by reference | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8495@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
Comment by: crmalibu at gmail dot com
Reported by: confidential99 at gmail dot com
Summary: array_replace() arguments not passed by reference
Status: Open
Type: Documentation Problem
Package: Arrays related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[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