#43424 [Bgs->Asn]: array_combine() doesn't truncate float values while using them as keys
| From: | jani@php.net | Date: | Wed, 28 Nov 2007 12:20:51 +0000 |
| Subject: | #43424 [Bgs->Asn]: array_combine() doesn't truncate float values while using them as keys | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-120170@lists.php.net to get a copy of this message | ||
ID: 43424
Updated by: jani@php.net
Reported By: dharma dot yp at in dot ibm dot com
-Status: Bogus
+Status: Assigned
Bug Type: Arrays related
Operating System: Linux, windows
PHP Version: 5.2CVS-2007-11-27 (snap)
-Assigned To:
+Assigned To: iliaa
New Comment:
See bug #29008 which fixed the behaviour from ignoring all other types
but integers. Now it just does convert_to_string() on any other than
integer. This is a bit tricky. IMO the current behaviour of
array_combine() is expected and should stay. It might be "unexpected"
but perhaps just documenting it as such would be sufficient. (I happen
to rely on this function to behave like this so changing it would make
me a sad puppy :)
Ilia, what do you think?
Previous Comments:
------------------------------------------------------------------------
[2007-11-27 12:56:02] jani@php.net
If you look closely at the var_dump() output you'll notice that they're
not floats but strings. This is expected and correct behaviour.
------------------------------------------------------------------------
[2007-11-27 12:43:38] dharma dot yp at in dot ibm dot com
Description:
------------
when array with float values is passed to $keys argument of
array_combine()function, it creates an array with float keys and the
values specified by $values argument. This is in contradiction with the
array documentation, which specifies that only integers or strings can
be used as keys in an array.Same can be found in the below link.
http://in2.php.net/manual/en/language.types.array.php
So, either of the below is expected:
1) An error message saying that float values can't be used as keys for
an array should be output or
2) float keys should be truncated to integer keys.
This behaviour can be found with PHP5.3 and PHP6 as well.
Reproduce code:
---------------
<?php
$arr1 = array(1.1, 2.2);
$arr2 = array(1, 2);
var_dump( array_combine($arr1, $arr2) );
?>
Expected result:
----------------
array(2) {
[1]=>
int(1)
[2]=>
int(2)
}
Actual result:
--------------
array(2) {
["1.1"]=>
int(1)
["2.2"]=>
int(2)
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=43424&edit=1