0

$array = array (0.1 => 'a', 0.2 => 'b');
echo count ($array);

It overwrites first array element by second, just because, I used float with 0.

And hence output of above code is 1, instead of 2.

Why PHP round array index down to 0 ?

Santosh S
  • 4,165
  • 6
  • 32
  • 37
  • 2
    I didn't know you could use floats for index keys. Probably both indexes are turned to 0 and merged. EDIT: it is: http://ideone.com/LIC0E – Damien Pirsy Mar 29 '12 at 11:37
  • 3
    Floats in key are truncated to integer. See http://stackoverflow.com/questions/4542234/working-with-an-array-with-period-in-key-value Convert keys to strings – dotoree Mar 29 '12 at 11:38
  • 1
    You could have a string index of "0.1" but it creates a whole heap of subtle problems if you forget – Mark Baker Mar 29 '12 at 11:43
  • Nothing is wrong with *PHP*, something is wrong with your *understanding* of PHP. When in doubt, consult the manual. – Piskvor left the building Mar 29 '12 at 11:44
  • You guys has taken my question in wrong way, I mean to say that this is not the case with other programming language, why PHP handle float in such weird way. I already askeda question on float in php, please check for more details http://stackoverflow.com/questions/3148937/compare-floats-in-php – Santosh S Mar 29 '12 at 11:56
  • @santosh: 1) *That* other question you link to, OTOH, will be "weird" in *any* of the current programming languages, because `float` is a weird type ;) 2) As for "Why does a programming language work this way and not that way," the answer usually is "for historical reasons." I don't think that PHP's creators frequent this site, as they'd be in a position to explain their decisions. – Piskvor left the building Mar 29 '12 at 20:41

9 Answers9

7

The array keys are interpreted as numeric, but numeric keys must be integers, Therefore, both float values are cast (truncated) to integer zero and 0.2 overwrites 0.1.

var_dump($array);
array(1) {
  [0]=>
  string(1) "b"
}

Make the array keys strings if you want to use non integer values:

$array = array ("0.1" => 'a', "0.2" => 'b');
echo count($array);
// 2

array(2) {
  ["0.1"]=>
  string(1) "a"
  ["0.2"]=>
  string(1) "b"
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
2

Only integer is allowed as key of the array.

See what we get if I print_r($array):

Array ( [0] => b )

However you can do like this:

$array = array ('0.1' => 'a', '0.2' => 'b');

Now print_r says this:

Array ( [0.1] => a [0.2] => b )
Alex Amiryan
  • 1,374
  • 1
  • 18
  • 30
1

Array indices cannot be floats. They must be either integers or strings. If you would try to var_dump($array); you would see that your array looks something like this:

array(1) { 
    [0]=> string(1) "b" 
}

You are effectively trying to set value for key 0 twice.

gintas
  • 2,118
  • 1
  • 18
  • 28
1

You cannot use floats as numeric keys. 0.1 and 0.2 both get converted to 0

Either you have to use integers or strings. Therefore, your options are:

$array = array ('0.1' => 'a', '0.2' => 'b');

Or:

$array = array (1 => 'a', 2 => 'b');
Alp
  • 29,274
  • 27
  • 120
  • 198
1

Let's see what the PHP's own excellent manual says about arrays (emphasis mine):

The key can either be an integer or a string. The value can be of any type.

Additionally the following key casts will occur: [...] Floats are also cast to integers, which means that the fractional part will be truncated.

So, if you look at your array:

<?php
$array = array (0.1 => 'a', 0.2 => 'b');
var_dump($array); // let's see what actually *is* in the array
echo count ($array);

you'll get this back:

array(1) {
  [0]=>
  string(1) "b"
}
1

So, first your array is { 0 => 'a' }, then becomes { 0 => 'b' }. The computer did exactly what you asked it to, even if not what you intended.

Possible solution: pass the array keys as strings - there is no conversion to int, and it works as expected.

$array = array ('0.1' => 'a', '0.2' => 'b');
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
0

You must use quote on non-integer keys

$array = array ('0.1' => 'a', '0.2' => 'b');
echo count($array);
safarov
  • 7,793
  • 2
  • 36
  • 52
0

You are storing 'a' into the 0.1th element and 'b' into the 0.2nd element. This is impossible. Array indexes must be integers.

Perhaps you are wanting to use associative arrays?

$array = array ('0.1' => 'a', '0.2' => 'b');
F21
  • 32,163
  • 26
  • 99
  • 170
0

As per the php.net document on arrays for having keys:

Additionally the following key casts will occur: 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.

i tried dumping and interpreting the result... 0.1 and 0.2 will be interpreted as 0 and the latter overwrites the former, end result is that the array key remains 0 and value is set as b.

Hence there's nothing wrong with this behavior.

optimusprime619
  • 754
  • 2
  • 17
  • 38
0

its because floats are casted to integers, so the second entry overwrites the first.

Actually you are doing this:

$array = array (0 => 'a', 0 => 'b');
echo count ($array);

Php.net Array: "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."

Extranion
  • 588
  • 8
  • 25