简体   繁体   English

PHP数组:整数索引与字符串索引

[英]PHP array: integer index vs string index

Is there any difference between integer index and string index of PHP arrays (except of course the fact that the latter is called associative array )? PHP数组的整数索引字符串索引之间是否有任何区别(当然除了后者称为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] to $intIndex[4] ? 在第一种情况下, $intIndex[0]$intIndex[4]什么?

From the manual (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 : 此外, 还会发生以下关键演员

  • Strings containing valid integers will be cast to the integer type . 包含有效整数的字符串将强制转换为整数类型 Eg 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. 例如,键“8”实际上将存储在8下。另一方面,“08”将不会被转换,因为它不是有效的十进制整数。
  • Floats are also cast to integers, which means that the fractional part will be truncated. 浮点数也会转换为整数,这意味着小数部分将被截断。 Eg the key 8.7 will actually be stored under 例如,密钥8.7实际上将存储在
      8. 8。
  • [...] [...]

That's not related with the fact that PHP arrays are sparse. 这与PHP数组稀疏的事实无关。

You can verify all this with var_dump() . 您可以使用var_dump()验证所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM