#32671 [Opn]: Array keys converted from float to integer
| From: | sniper@php.net | Date: | Mon, 11 Apr 2005 15:45:21 +0000 |
| Subject: | #32671 [Opn]: Array keys converted from float to integer | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969367697@lists.php.net to get a copy of this message | ||
ID: 32671
Updated by: sniper@php.net
Reported By: marek at lewczuk dot com
Status: Open
-Bug Type: Arrays related
+Bug Type: Documentation problem
Operating System: Linux
PHP Version: 5.0.3
New Comment:
This is nothing new for PHP 5, same happens with PHP 4.
Previous Comments:
------------------------------------------------------------------------
[2005-04-11 16:24:44] marek at lewczuk dot com
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.
------------------------------------------------------------------------
[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