一个数字字符串作为PHP中的数组键

问题描述:

是否可以将像"123"这样的数字字符串用作PHP数组中的键,而不必将其转换为整数?

Is it possible to use a numeric string like "123" as a key in a PHP array, without it being converted to an integer?

$blah = array('123' => 1);
var_dump($blah);

打印

array(1) {
  [123]=>
  int(1)
}

我想要

array(1) {
  ["123"]=>
  int(1)
}

否;不,不是:

手册:

键可以是整数或字符串.如果键是整数的标准表示形式,它将被解释为整数(即​​"8"将被解释为8,而"08"将被解释为"08").

A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08").

附录

由于下面的注释,我认为指出该行为与JavaScript对象键相似而不是相同会很有趣.

Because of the comments below, I thought it would be fun to point out that the behaviour is similar but not identical to JavaScript object keys.

foo = { '10' : 'bar' };

foo['10']; // "bar"
foo[10]; // "bar"
foo[012]; // "bar"
foo['012']; // undefined!