Bug #54140 [Opn->Nab]: array_merge() break keys during merge

From: Date: Fri, 08 May 2015 22:07:34 +0000
Subject: Bug #54140 [Opn->Nab]: array_merge() break keys during merge
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-192594@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=54140&edit=1

 ID:                 54140
 Updated by:         cmb@php.net
 Reported by:        forjest at gmail dot com
 Summary:            array_merge() break keys during merge
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Arrays related
 Operating System:   *Nix
 PHP Version:        5.3.5
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

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>


Previous Comments:
------------------------------------------------------------------------
[2012-01-04 17:03:50] siedler1 at freenet dot de

I have php version 5.3.6 on Ubuntu and this behavior still exist.

------------------------------------------------------------------------
[2011-04-20 17:09:29] martin at fleveo dot cz

This bug is on Windows 7 too (same version of PHP).

------------------------------------------------------------------------
[2011-03-09 19:27:38] 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-03-02 19:03:46] forjest at gmail dot com

Works as expected on Windows platform.

------------------------------------------------------------------------
[2011-03-02 18:21:06] 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)


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



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


Thread (6 messages)

« previous php.bugs (#192594) next »