#43424 [Asn->Bgs]: array_combine() doesn't truncate float values while using them as keys
| From: | iliaa@php.net | Date: | Wed, 28 Nov 2007 14:39:37 +0000 |
| Subject: | #43424 [Asn->Bgs]: 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-120180@lists.php.net to get a copy of this message | ||
ID: 43424
Updated by: iliaa@php.net
Reported By: dharma dot yp at in dot ibm dot com
-Status: Assigned
+Status: Bogus
Bug Type: Arrays related
Operating System: Linux, windows
PHP Version: 5.2CVS-2007-11-27 (snap)
Assigned To: iliaa
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
I believe that the current behavior is the correct one. Converting
float
to an int for the purposes of array keys can result in a silent loss of
data.
Consider the following example:
$foo = array(1 => 123, 1.1 => 245);
If the float is converted to an int, the contents of $foo will be
array(1 => 245) resulting in data loss. This is why the change was made
back in the day.
Previous Comments:
------------------------------------------------------------------------
[2007-11-28 12:20:50] jani@php.net
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?
------------------------------------------------------------------------
[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