1

I am using php map for storing values in array map but when i increase the value for key, value of other key gets increased automatically.

Error while calculating

$myhashmap = array();
$myhashmap[0.2] = 1;
$myhashmap[0.1] = 2;
$myhashmap[0.1]++;
echo $myhashmap[0.1]."\n";   // 3 expected
$myhashmap[0.2]++;
echo $myhashmap[0.1]."\n";   // 4 unexpected 

Like in above case in line6, i am increasing values corresponding to 0.2 but value corresponding to 0.1 gets increased. How can i fix this error?

Manish
  • 3,341
  • 15
  • 52
  • 87
  • 1
    Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8. http://php.net/manual/en/language.types.array.php. related http://stackoverflow.com/questions/4542234/working-with-an-array-with-periods-in-key-values – Kevin Jan 13 '15 at 08:04
  • Do i need to typecast everytime? – Manish Jan 13 '15 at 08:07
  • 1
    no, you only need them quoted. `$myhashmap['0.2']` etc.. – Kevin Jan 13 '15 at 08:09

0 Answers0