php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54140 array_merge() break keys during merge
Submitted: 2011-03-02 18:21 UTC Modified: 2015-05-08 22:07 UTC
Votes:10
Avg. Score:4.2 ± 1.0
Reproduced:5 of 5 (100.0%)
Same Version:2 (40.0%)
Same OS:4 (80.0%)
From: forjest at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Arrays related
PHP Version: 5.3.5 OS: *Nix
Private report: No CVE-ID: None
 [2011-03-02 18:21 UTC] forjest at gmail dot com
Description:
------------
array_merge interprets string keys, containing only digits as numeric keys, if numeric overflow not reached.

String keys should works as strings regarless of their contents.


Test script:
---------------
var_dump(array_merge(array('t777'=> array("I'm a string"))));
var_dump(array_merge(array("42424242"=> array("I'm a string of digits"))));
var_dump(array_merge(array("777777777777777777777777777777777777777777777777777777777"=> array("I'm string of digits too"))));

var_dump(is_string("42424242"));
var_dump(is_string(42424242));
var_dump(is_string("777777777777777777777777777777777777777777777777777777777"));


Expected result:
----------------
array(1) {
  ["t777"]=>
  array(1) {
    [0]=>
    string(12) "I'm a string"
  }
}
array(1) {
  ["42424242"]=>
  array(1) {
    [0]=>
    string(22) "I'm a string of digits"
  }
}
array(1) {
  ["777777777777777777777777777777777777777777777777777777777"]=>
  array(1) {
    [0]=>
    string(24) "I'm string of digits too"
  }
}
bool(true)
bool(false)
bool(true)

Actual result:
--------------
array(1) {
  ["t777"]=>
  array(1) {
    [0]=>
    string(12) "I'm a string"
  }
}
array(1) {
  [0]=>
  array(1) {
    [0]=>
    string(22) "I'm a string of digits"
  }
}
array(1) {
  ["777777777777777777777777777777777777777777777777777777777"]=>
  array(1) {
    [0]=>
    string(24) "I'm string of digits too"
  }
}
bool(true)
bool(false)
bool(true)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-02 19:03 UTC] forjest at gmail dot com
-Operating System: +Operating System: *Nix
 [2011-03-02 19:03 UTC] forjest at gmail dot com
Works as expected on Windows platform.
 [2011-03-09 19:27 UTC] cybd at bigmir dot net
Maybe, simpler example.

OK:
    $c = array('01'=>3, '03'=>10);
    $d = array('05'=>44, '07'=>3);

    var_dump(array_merge($c,$d));

Failed:
    $a = array('01'=>1, '02'=>73);
    $b = array('10'=>11, '11'=>23);
    
    var_dump(array_merge($a,$b));
 [2011-04-20 17:09 UTC] martin at fleveo dot cz
This bug is on Windows 7 too (same version of PHP).
 [2012-01-04 17:03 UTC] siedler1 at freenet dot de
I have php version 5.3.6 on Ubuntu and this behavior still exist.
 [2015-05-08 22:07 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-05-08 22:07 UTC] cmb@php.net
This is not particularly related to array_merge(), but to the
handling of array keys in general. From the manual[1]:

| Strings containing valid integers will be cast to the integer
| type.

So

  var_dump(array('10'=>11, '11'=>23));
  
outputs:

  array(2) {
    [10] =>
    int(11)
    [11] =>
    int(23)
   }

and not:

  array(2) {
    '10' =>
    int(11)
    '11' =>
    int(23)
  }
  
The rest is documented on the array_merge() man page:

| Values in the input array with numeric keys will be renumbered
| with incrementing keys starting from zero in the result array.

[1] <http://php.net/manual/en/language.types.array.php>
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Aug 10 16:01:23 2020 UTC