#20381 [Ver]: array_merge_recursive mangles input arrays

From: Date: Tue, 12 Nov 2002 13:22:52 +0000
Subject: #20381 [Ver]: array_merge_recursive mangles input arrays
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-24691@lists.php.net to get a copy of this message
 ID:               20381
 Updated by:       sniper@php.net
 Reported By:      mailfrom-bugs.php.net@kopka.net
 Status:           Verified
 Bug Type:         Arrays related
 Operating System: SuSE Linux 7.3
 PHP Version:      4.3.0-dev
 New Comment:

They're actually mangled with CVS HEAD too..somehow I missed that or
had those print_r() lines in wrong place. :I



Previous Comments:
------------------------------------------------------------------------

[2002-11-12 07:16:46] mailfrom-bugs.php.net@kopka.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 06:33:56] 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-11 23:04:18] mailfrom-bugs.php.net@kopka.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
                        )

                )

        )

)


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=20381&edit=1



Thread (5 messages)

« previous php.bugs (#24691) next »