php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75483 array_replace_recursive will merge (not replace), when both values are arrays
Submitted: 2017-11-03 15:08 UTC Modified: 2017-11-03 15:16 UTC
From: gladykov at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.1.11 OS:
Private report: No CVE-ID: None
 [2017-11-03 15:08 UTC] gladykov at gmail dot com
Description:
------------
array_replace_recursive will merge (not replace) values, when both key values in original and replacement matching key are arrays.

When one of values either in original key or replacement key is string, replacement will occur properly.



Test script:
---------------
<?php

//If value of availableReport key will be string in $original OR $replacement array_replace_recursive will work properly

$original = [
'availableReports' => ['InternalReview' => 'Internal Review'],
];


$replacements = [
    'availableReports' => ['AppraisalRev' => 'Commercial Appraisal Review']
];

//Will print both values in current implementation
print_r(array_replace_recursive($original, $replacements));

?>


Expected result:
----------------
Array
(
    [availableReports] => Array
        (
            [AppraisalRev] => Commercial Appraisal Review
        )
)


Actual result:
--------------
Array
(
    [availableReports] => Array
        (
            [InternalReview] => Internal Review
            [AppraisalRev] => Commercial Appraisal Review
        )
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-11-03 15:16 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-11-03 15:16 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Well, yeah. Because it's recursive. Wouldn't be much point to a recursive function that doesn't act recursively.

Perhaps you want to use the regular array_replace?
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Apr 10 03:01:24 2020 UTC