PHP array: integer index and string index
Is there a difference between an integer index and a string index of PHP arrays (except, of course, that the latter is called associative array
)?
So, for example, what are the differences between the following two arrays:
$intIndex[5] = "Hello";
$intIndex[6] = "World";
$intIndex[7] = "!";
and
$strIndex['5'] = "Hello";
$strIndex['6'] = "World";
$strIndex['7'] = "!";
In the first case, what happens to $intIndex[0]
before $intIndex[4]
?
+3
source to share
1 answer
From the manual (emphasis mine):
The key can be an integer or a string. The value can be of any type.
In addition, the following key casts will be performed :
[...]
- Strings containing real integers will be cast to an integer type . For example. the key "8" will actually be stored under 8. On the other, manual "08" will not be selected since it is not a valid decimal integer.
- Floats are also cast from integers, which means that the fractional part will be truncated. For example. the key 8.7 will actually be stored in
- 8.
This is not due to the fact that PHP arrays are sparse.
You can check all of this with var_dump () .
+2
source to share