PHP数组的整数索引和字符串索引有什么区别吗(当然除了后者叫做associative array
)?
例如,下面两个数组有什么区别:
$intIndex[5] = "Hello";
$intIndex[6] = "World";
$intIndex[7] = "!";
和
$strIndex['5'] = "Hello";
$strIndex['6'] = "World";
$strIndex['7'] = "!";
在第一种情况下,$intIndex[0]
到 $intIndex[4]
会怎样?
最佳答案
来自 the manual (强调我的):
The key can either be an integer or a string. The value can be of any type.
Additionally the following key casts will occur:
- Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
- 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.
- [...]
这与 PHP 数组是稀疏的事实无关。
您可以使用 var_dump() 验证所有这些.
关于PHP 数组 : integer index vs string index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26119524/