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
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: 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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri May 10 17:01:34 2024 UTC