#32671 [Opn->Csd]: Array keys converted from float to integer
| From: | vrana@php.net | Date: | Tue, 12 Apr 2005 07:41:36 +0000 |
| Subject: | #32671 [Opn->Csd]: Array keys converted from float to integer | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969367744@lists.php.net to get a copy of this message | ||
ID: 32671
Updated by: vrana@php.net
Reported By: marek at lewczuk dot com
-Status: Open
+Status: Closed
Bug Type: Documentation problem
Operating System: Linux
PHP Version: 5.0.3
New Comment:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation
better.
"Floats in key are truncated to integer."
Previous Comments:
------------------------------------------------------------------------
[2005-04-11 20:46:17] marek at lewczuk dot com
Maybe is same for PHP4 - I just never had a chance check how this
works. So, I understand that this is a proper behavior, right ?
------------------------------------------------------------------------
[2005-04-11 17:45:20] sniper@php.net
This is nothing new for PHP 5, same happens with PHP 4.
------------------------------------------------------------------------
[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