#32671 [Opn]: Array keys converted from float to integer
| From: | marek at lewczuk dot com | Date: | Mon, 11 Apr 2005 14:24:45 +0000 |
| Subject: | #32671 [Opn]: Array keys converted from float to integer | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-76639@lists.php.net to get a copy of this message | ||
ID: 32671
User updated by: marek at lewczuk dot com
Reported By: marek at lewczuk dot com
Status: Open
Bug Type: Arrays related
Operating System: Linux
PHP Version: 5.0.3
New Comment:
One more example, what problem this "bug/behavior" may cause:
$value1 = 345.654;
$value2 = 345.655;
$array = array();
$array[$value1] = $value1;
$array[$value2] = $value2;
Result:
array (
345 => 345.655,
)
If this is a normal behavior you should put a note about this in the
manual.
Previous Comments:
------------------------------------------------------------------------
[2005-04-11 16:19:04] marek at lewczuk dot com
Description:
------------
Suppose we have a float value: $value = 345.332 and we want to use this
value as an array key: $array[$value] = $value. This will cause that key
will be truncated to integer 345 not to string "345.332". It is written
in the manual that:
"...A key may be either an integer or a string. If a key is the
standard representation of an integer, it will be interpreted as such
(i.e. "8" will be interpreted as 8, while "08" will be interpreted as
"08")..."
From this point of view floats should be converted to strings. I'm not
saying that this is a bug, rather I would like to be sure if this is a
proper behavior. Simple solution is to type cast float value to string
(string), but we have to know that given value is a float (and
sometimes, we don't know that). IMHO any other type than integer should
be treat as string.
Reproduce code:
---------------
$value = 345.654;
$array = array();
$array[$value] = "Float";
Expected result:
----------------
array (
"345.654" => "Float"
)
Actual result:
--------------
array (
"345" => "Float"
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=32671&edit=1