#32671 [NEW]: Array keys converted from float to integer
| From: | marek at lewczuk dot com | Date: | Mon, 11 Apr 2005 14:19:05 +0000 |
| Subject: | #32671 [NEW]: Array keys converted from float to integer | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-76638@lists.php.net to get a copy of this message | ||
From: marek at lewczuk dot com
Operating system: Linux
PHP version: 5.0.3
PHP Bug Type: Arrays related
Bug description: Array keys converted from float to integer
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 bug report at http://bugs.php.net/?id=32671&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=32671&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=32671&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=32671&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=32671&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=32671&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=32671&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=32671&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=32671&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=32671&r=support
Expected behavior: http://bugs.php.net/fix.php?id=32671&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=32671&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=32671&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=32671&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=32671&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=32671&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=32671&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=32671&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=32671&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=32671&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=32671&r=mysqlcfg