php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14990 array_merge_recursive modifies inputted value
Submitted: 2002-01-11 04:02 UTC Modified: 2002-11-12 04:39 UTC
Votes:7
Avg. Score:4.7 ± 0.5
Reproduced:3 of 4 (75.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: philip at cornado dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 4.1.1 OS: n/a
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: philip at cornado dot com
New email:
PHP Version: OS:

 

 [2002-01-11 04:02 UTC] philip at cornado dot com
In Summary:
--------------
array_merge_recursive() modifies the array entered as the second parameter if the merged arrays have at least one identical stringed key.  It will affect all such duplicate keys, and modify the second parameter's array as demonstrated below.

Example:
--------------
<?php
  $a = array(2 => '2 a', 'foo' => 'foo a','bar' => 'bar a');
  $b = array(2 => '2 b', 'foo' => 'foo b','bar' => 'bar b');

  $c['recursive'] = array_merge_recursive($a,$b);
  $c['a'] = $a;
  $c['b'] = $b;

  print_r($c);
?>

Example output:
---------------
Array
(
    [recursive] => Array
        (
            [0] => 2 a
            [foo] => Array
                (
                    [0] => foo a
                    [1] => foo b
                )

            [bar] => Array
                (
                    [0] => bar a
                    [1] => bar b
                )

            [1] => 2 b
        )

    [a] => Array
        (
            [2] => 2 a
            [foo] => foo a
            [bar] => bar a
        )

    [b] => Array
        (
            [2] => 2 b
            [foo] => Array
                (
                    [0] => foo b
                )

            [bar] => Array
                (
                    [0] => bar b
                )

        )

)

Notes:
----------------
Notice how in $b, keys 'foo' and 'bar' are now arrays when initially they were not.  This behavior exists with all such duplicate stringed keys.  array_merge_recursive() should not directly affect inputted values.  This seems related to bug
#14128.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-11 04:32 UTC] philip at cornado dot com
With 3+ parameters, only the first parameter is left untouched.  All others are affected, as demonstrated above with $b.
 [2002-04-24 11:02 UTC] andrei@php.net
This bug has been fixed in CVS.


 [2002-11-12 04:39 UTC] philip@php.net
It's fixed in CVS and PHP 4.3.0-dev is showing correct bahavior.  Will add documention shortly.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 06:01:32 2024 UTC