php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43424 array_combine() doesn't truncate float values while using them as keys
Submitted: 2007-11-27 12:43 UTC Modified: 2007-11-28 14:39 UTC
From: dharma dot yp at in dot ibm dot com Assigned: iliaa (profile)
Status: Not a bug Package: Arrays related
PHP Version: 5.2CVS-2007-11-27 (snap) OS: Linux, windows
Private report: No CVE-ID: None
 [2007-11-27 12:43 UTC] 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)
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-11-27 12:56 UTC] 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-28 12:20 UTC] 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-28 14:39 UTC] iliaa@php.net
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.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Oct 22 13:01:24 2020 UTC