php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20381 array_merge_recursive mangles input arrays
Submitted: 2002-11-11 23:04 UTC Modified: 2002-11-15 07:38 UTC
From: mailfrom-bugs dot php dot net at kopka dot net Assigned:
Status: Closed Package: Arrays related
PHP Version: 4.3.0-dev OS: SuSE Linux 7.3
Private report: No CVE-ID: None
 [2002-11-11 23:04 UTC] mailfrom-bugs dot php dot net at kopka dot net
Similar to #14990 (exept that the demo code there runs fine) and the first source array gets mangled.

 Example
=========
<pre><?

$a = array(
	'a1' => 1,
	'a2' => array( 1, 2, 3 ),
	'a3' => array(
		'a' => array( 10, 20, 30 ),
		'b' => 'b'
		)
	);
$b = array( 'a1' => 2,
	'a2' => array( 3, 4, 5 ),
	'a3' => array(
		'c' => 'cc',
		'a' => array( 10, 40 )
		)
	);

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

print_r( $c );

?>


 Example Output
================
Array
(
    [result] => Array
        (
            [a1] => Array
                (
                    [0] => 1
                    [1] => 2
                )

            [a2] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 3
                    [4] => 4
                    [5] => 5
                )

            [a3] => Array
                (
                    [a] => Array
                        (
                            [0] => 10
                            [1] => 20
                            [2] => 30
                            [3] => 10
                            [4] => 40
                        )

                    [b] => b
                    [c] => cc
                )

        )

    [a] => Array
        (
            [a1] => 1
            [a2] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 3
                    [4] => 4
                    [5] => 5
                )

            [a3] => Array
                (
                    [a] => Array
                        (
                            [0] => 10
                            [1] => 20
                            [2] => 30
                            [3] => 10
                            [4] => 40
                        )

                    [b] => b
                    [c] => cc
                )

        )

    [b] => Array
        (
            [a1] => 2
            [a2] => Array
                (
                    [0] => 3
                    [1] => 4
                    [2] => 5
                )

            [a3] => Array
                (
                    [c] => cc
                    [a] => Array
                        (
                            [0] => 10
                            [1] => 40
                        )

                )

        )

)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-12 06:33 UTC] sniper@php.net
Reproduced using latest CVS. The original arrays are fine, but when they're added to the resulting array, then they get mangled.

 [2002-11-12 07:16 UTC] mailfrom-bugs dot php dot net at kopka dot net
In 4.3.2 the source arrays are mangled even when you don't add them to the result.

This was just for a nicer, smaller example.

Example
========

// Same $a and $b as before

$result = array_merge_recursive( $a, $b );
print_r( $c );
print_r( $a );
print_r( $b );

// have sadly the same effect
 [2002-11-12 07:22 UTC] sniper@php.net
They're actually mangled with CVS HEAD too..somehow I missed that or had those print_r() lines in wrong place. :I

 [2002-11-15 07:38 UTC] moriyoshi@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, 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/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Oct 15 22:01:26 2024 UTC