Santosh S
Santosh S

Reputation: 4345

why i am getting wrong array count in php

$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 ?

Upvotes: 0

Views: 2546

Answers (9)

Piskvor left the building
Piskvor left the building

Reputation: 92792

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');

Upvotes: 1

Extranion
Extranion

Reputation: 608

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."

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270775

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"
}

Upvotes: 7

optimusprime619
optimusprime619

Reputation: 764

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.

Upvotes: 0

Alex Amiryan
Alex Amiryan

Reputation: 1382

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 )

Upvotes: 2

Alp
Alp

Reputation: 29749

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');

Upvotes: 1

gintas
gintas

Reputation: 2178

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.

Upvotes: 1

F21
F21

Reputation: 33441

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');

Upvotes: 0

safarov
safarov

Reputation: 7804

You must use quote on non-integer keys

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

Upvotes: 0

Related Questions