PHP数组:整数索引VS字符串索引 [英] PHP array: integer index vs string index

查看:267
本文介绍了PHP数组:整数索引VS字符串索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何区别的整数索引字符串指数 PHP数组(的当然除了一个事实,即后者被称为关联数组)?

因此​​,例如,什么是以下两个阵列之间的差异:

  $ intIndex [5] =你好;
$ intIndex [6] =世界;
$ intIndex [7] =!;

  $ strIndex ['5'] =你好;
$ strIndex ['6'] =世界;
$ strIndex ['7'] =!;

在第一种情况下,会发生什么 $ intIndex [0] $ intIndex [4] <? / p>

解决方案

手动(重点煤矿):


  

键可以是整数或字符串。的值可以是任何
  类型。


  
  

此外下列键的演员将出现


  
  

      
  • 包含有效整数将转换为整数类型字符串。例如。键8实际上将8下被存储在另一
      手08不会被投射,因为它不是一个有效的十进制整数。

  •   
  • 花车也被转换为整数,这意味着小数部分将被截去。例如。关键8.7实际上下储存
      <醇开始=8>
      8。
      

  •   
  • [...]

  •   

这是不是与事实PHP数组是稀疏相关的。

您可以验证这一点所有的的var_dump()

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

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

解决方案

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

  • [...]

That's not related with the fact that PHP arrays are sparse.

You can verify all this with var_dump().

这篇关于PHP数组:整数索引VS字符串索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆