$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 ?
$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 ?
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"
}
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 )
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.
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');
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');
You must use quote on non-integer keys
$array = array ('0.1' => 'a', '0.2' => 'b');
echo count($array);
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');
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.
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."