PHP 数组 : integer index vs string index

标签 php arrays memory integer associative-array

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/

相关文章:

PHP 上传表单、PDF、Doc 和 Docx

php - PDO 语句属性存储为对象属性并通过对象方法访问

java - 动态数组长度

c - 进程地址空间中的 Linux 变量存储 - C

php - LAMP 服务器上的函数自动加载类错误

C#数组题(拆分)

javascript - 如何使用动态 JSON 中的键来创建 javascript 数组?

c++ - 当您的应用程序开始耗尽内存时,您会怎么做?

c - 动态数组和结构 C 的 Valgrind 内存泄漏

php - 确定给定的外国 ID 是否由用户拥有?