php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27644 array_merge reverses elements in first array
Submitted: 2004-03-18 20:25 UTC Modified: 2004-03-26 14:45 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:0 (0.0%)
From: ashok at yahoo-inc dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 4.3.4 OS: FreeBsd 4.3
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ashok at yahoo-inc dot com
New email:
PHP Version: OS:

 

 [2004-03-18 20:25 UTC] ashok at yahoo-inc dot com
Description:
------------
When using array_merge to merge 2 arrays, the order
of elements of the first are changed. The keys are all integers unlike other existing bugs where keys are mixed integers and strings.

Reproduce code:
---------------
<?php
    $string1 = "1|Text|0-0-0-0-0-0-0";
    list ($arr1[0], $arr1[1], $string2) = explode ("|", $string1, 3);

    print ("Values of arr1 after first explode...\n");
    for ($i = 0; $i < count ($arr1); $i++)
        print ("arr1[$i] = " . $arr1[$i] . "\n");

    print ("\nValues of arr1 after second explode...\n");
    $arr1 = array_merge ($arr1, explode ('-', $string2));
    for ($i = 0; $i < count ($arr1); $i++)
        print ("arr1[$i] = " . $arr1[$i] . "\n");
?>
~

Expected result:
----------------
arr1[0] = 1
arr1[1] = Text
arr1[2] = 0
arr1[3] = 0
arr1[4] = 0
arr1[5] = 0
arr1[6] = 0
arr1[7] = 0
arr1[8] = 0

Actual result:
--------------
arr1[0] = Text
arr1[1] = 1
arr1[2] = 0
arr1[3] = 0
arr1[4] = 0
arr1[5] = 0
arr1[6] = 0
arr1[7] = 0
arr1[8] = 0

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-23 13:37 UTC] iliaa@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

Works just fine. I've replaced your print code with 
print_r(), which shows the correct order and here is what 
you get: 
 
Array 
( 
    [1] => Text 
    [0] => 1 
) 
Array 
( 
    [0] => Text 
    [1] => 1 
    [2] => 0 
    [3] => 0 
    [4] => 0 
    [5] => 0 
    [6] => 0 
    [7] => 0 
    [8] => 0 
) 
 
 [2004-03-24 21:02 UTC] ashok at yahoo-inc dot com
Please see the index in your output. The first output shows array[1] before array[0]. I think it should be top priority as it is changing the array elments under you and you don't expect that. It's a random behaviour. 

Thanks.

-Ashok.
 [2004-03-26 14:45 UTC] iliaa@php.net
The problem is fixed in PHP5, however it cannot be 
addressed in PHP 4 and hence marked won't fix. 
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 08:01:34 2025 UTC