php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #5970 array_merge() "forgets" some keys in associative arrays
Submitted: 2000-08-04 18:10 UTC Modified: 2000-08-05 12:08 UTC
From: marc at skirando dot ch Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 4.0.1pl2 OS: Linux 2.2.14 (RedHat 6.2)
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: marc at skirando dot ch
New email:
PHP Version: OS:

 

 [2000-08-04 18:10 UTC] marc at skirando dot ch
When keys in associative arrays are only composed of integers, the concerned lines of the table are converted to non-associative lines, after passing through array_merge(). With keys composed of strings or mixed integers-strings, this misbehaviour doesn't occur.

$array1 = array ("foo" => "bar", "bla" => "blabla");
$array2 = array ("123" => "one two three", "456" => "four five six");

while (list ($key, $val) = each ($array1)) print "$key : $val \n";
  /* this returns :
foo : bar
bla : blabla
  it's correct */

while (list ($key, $val) = each ($array2)) print "$key : $val \n";
   /* this returns :
123 : one two three
456 : four five six
  it's also correct */

$array3 = array_merge ($array1, $array2);
while (list ($key, $val) = each ($array3)) print "$key : $val, ";
   /* this returns :
foo : bar
bla : blabla
0 : one two three
1 : four five six
   strange, isn't it ? */

Note: I've installed mandrake-cooker rpm's of php4, as the rpms shipped with redhat 6.2 are only old 3.something. Perhaps this can be the cause of the problem ? I haven't tried to reproduce the bug on another system.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-05 12:08 UTC] stas@php.net
array_merge does not preserve bumeric keys, this is by design.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 09 23:01:33 2025 UTC