不能使用带有数字键作为字符串的PHP数组[duplicate]

时间:2021-01-05 08:43:51

This question already has an answer here:

这个问题已经有了答案:

I have a PHP array that has numeric keys as a string type.

我有一个PHP数组,它的数字键作为字符串类型。

But when I try and access them PHP is giving me an undefined index error.

但是当我尝试访问它们时PHP会给我一个未定义的索引错误。

$a = (array)json_decode('{"1":1,"2":2}');
var_dump($a);
var_dump(isset($a[1]));
var_dump(isset($a["1"]));
var_dump($a[1]);
var_dump($a["1"]);

Output:

输出:

array (size=2)
    '1' => int 1
    '2' => int 2

boolean false

boolean false

ERROR: E_NOTICE: Undefined offset: 1

null

ERROR: E_NOTICE: Undefined offset: 1

null

How do I access these values?

如何访问这些值?

Demo: http://codepad.viper-7.com/8O03IM

演示:http://codepad.viper com/8o03im——7.

6 个解决方案

#1


28  

So, I haven't seen any other answers touch upon this, but @xdazz came close.

所以,我还没有看到其他的答案涉及到这一点,但是@xdazz接近了。

Let's start our environment, $obj equals the object notation of a decoded string:

让我们开始我们的环境,$obj等于解码字符串的对象表示法:

php > $obj = json_decode('{"1":1,"2":2}');

php > print_r($obj);
stdClass Object
(
    [1] => 1
    [2] => 2
)

php > var_dump( $obj );
object(stdClass)#1 (2) {
  ["1"]=>
  int(1)
  ["2"]=>
  int(2)
}

If you want to access the strings, we know the following will fail:

如果您想要访问字符串,我们知道以下将失败:

php > echo $obj->1;

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'{'' or `'$'' in php shell code on line 1

Accessing the object variables

访问对象变量

You can access it like so:

你可以这样访问它:

php > echo $obj->{1};
1

Which is the same as saying:

也就是说

php > echo $obj->{'1'};
1

Accessing the array variables

访问数组变量

The issue with arrays is that the following return blank, which is the issue with typecasting.

数组的问题是返回空,这是类型转换的问题。

php > echo $obj[1];
php >

If you typecast it back, the object is once again accessible:

如果你把它重新排版,对象就可以再次访问:

php > $obj = (object) $obj;
php > echo $obj->{1};
1

Here is a function which will automate the above for you:

这里有一个功能,可以为您自动完成上述工作:

function array_key($array, $key){
    $obj = (object) $array;
    return $obj->{$key};
}

Example usage:

使用示例:

php > $obj = (array) $obj;
php > echo array_key($obj, 1);
1

php > echo array_key($obj, 2);
2

#2


25  

If you want array, set the second parameter of json_decode to true.

如果需要数组,请将json_decode的第二个参数设置为true。

$a = json_decode('{"1":1,"2":2}', true);

Edit: when you cast a std object to array, numeric string key doesn't cast to number. Here is an example.

编辑:当您向数组强制转换std对象时,数字字符串键不会强制转换为数字。这是一个例子。

$obj = new stdClass;
$obj->{'1'} = 1;
$arr = (array) $obj;
var_dump($arr);
var_dump(isset($arr[1]));

#3


14  

Apparently, this is a known issue, and there are no plans to fix it; see Doc Bug #45959 Object to array conversion leads to weird behaviour:

显然,这是一个已知的问题,没有计划去解决它;查看Doc Bug #45959对象到数组的转换会导致奇怪的行为:

Fixing that implies a perfomance decrease, hence the better seems be keep it as an known issue, but documented.

修正它意味着性能降低,因此更好的方法是将它作为一个已知的问题,但是要有文档记录。

This wonkiness is (then) noted in the documentation for the array type (emphasis mine):

在数组类型的文档中(强调我的)指出了这一奇怪之处:

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible.... This can result in some unexpected behaviour....

如果一个对象被转换为一个数组,那么结果就是一个数组,其元素就是对象的属性。键是成员变量名,有一些明显的例外:整数属性是达不到的....这可能导致一些意想不到的行为....

#4


1  

Yes. I do agree, PHP has issue with typecasting from object to array but foreach is handling intelligently the object or associative array.

是的。我确实同意,PHP有从对象到数组的类型转换的问题,但是foreach正在智能地处理对象或关联数组。

$a = json_decode('{"1":1,"2":2}'); //need not typecast but doesnt break even if u typecast

foreach ($a as $k=>$v){
    echo $v;
}

#5


1  

I met the same problem recently.

我最近遇到了同样的问题。

$obj = new stdClass();
$obj->{'0'} = "test";

$array = (array)$obj;
foreach ($array as $key => $value) {
   $array[$key] = strtoupper($value);
}
var_dump($array);

This code outputs :

这段代码输出:

array(2) {
  ["0"]=>
  string(4) "test"
  [0]=>
  string(4) "TEST"
}

Found that when debugging a method to convert recursivly objects to array, I've been mad.

发现当调试一种方法将递归的对象转换为数组时,我已经疯了。

#6


1  

I had the same problem (but with array_intersect_key).

我也遇到了同样的问题(但使用array_intersect_key)。

Here is my solution:

这是我的解决方案:

$array = array_combine(array_keys($array), $array);

#1


28  

So, I haven't seen any other answers touch upon this, but @xdazz came close.

所以,我还没有看到其他的答案涉及到这一点,但是@xdazz接近了。

Let's start our environment, $obj equals the object notation of a decoded string:

让我们开始我们的环境,$obj等于解码字符串的对象表示法:

php > $obj = json_decode('{"1":1,"2":2}');

php > print_r($obj);
stdClass Object
(
    [1] => 1
    [2] => 2
)

php > var_dump( $obj );
object(stdClass)#1 (2) {
  ["1"]=>
  int(1)
  ["2"]=>
  int(2)
}

If you want to access the strings, we know the following will fail:

如果您想要访问字符串,我们知道以下将失败:

php > echo $obj->1;

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'{'' or `'$'' in php shell code on line 1

Accessing the object variables

访问对象变量

You can access it like so:

你可以这样访问它:

php > echo $obj->{1};
1

Which is the same as saying:

也就是说

php > echo $obj->{'1'};
1

Accessing the array variables

访问数组变量

The issue with arrays is that the following return blank, which is the issue with typecasting.

数组的问题是返回空,这是类型转换的问题。

php > echo $obj[1];
php >

If you typecast it back, the object is once again accessible:

如果你把它重新排版,对象就可以再次访问:

php > $obj = (object) $obj;
php > echo $obj->{1};
1

Here is a function which will automate the above for you:

这里有一个功能,可以为您自动完成上述工作:

function array_key($array, $key){
    $obj = (object) $array;
    return $obj->{$key};
}

Example usage:

使用示例:

php > $obj = (array) $obj;
php > echo array_key($obj, 1);
1

php > echo array_key($obj, 2);
2

#2


25  

If you want array, set the second parameter of json_decode to true.

如果需要数组,请将json_decode的第二个参数设置为true。

$a = json_decode('{"1":1,"2":2}', true);

Edit: when you cast a std object to array, numeric string key doesn't cast to number. Here is an example.

编辑:当您向数组强制转换std对象时,数字字符串键不会强制转换为数字。这是一个例子。

$obj = new stdClass;
$obj->{'1'} = 1;
$arr = (array) $obj;
var_dump($arr);
var_dump(isset($arr[1]));

#3


14  

Apparently, this is a known issue, and there are no plans to fix it; see Doc Bug #45959 Object to array conversion leads to weird behaviour:

显然,这是一个已知的问题,没有计划去解决它;查看Doc Bug #45959对象到数组的转换会导致奇怪的行为:

Fixing that implies a perfomance decrease, hence the better seems be keep it as an known issue, but documented.

修正它意味着性能降低,因此更好的方法是将它作为一个已知的问题,但是要有文档记录。

This wonkiness is (then) noted in the documentation for the array type (emphasis mine):

在数组类型的文档中(强调我的)指出了这一奇怪之处:

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible.... This can result in some unexpected behaviour....

如果一个对象被转换为一个数组,那么结果就是一个数组,其元素就是对象的属性。键是成员变量名,有一些明显的例外:整数属性是达不到的....这可能导致一些意想不到的行为....

#4


1  

Yes. I do agree, PHP has issue with typecasting from object to array but foreach is handling intelligently the object or associative array.

是的。我确实同意,PHP有从对象到数组的类型转换的问题,但是foreach正在智能地处理对象或关联数组。

$a = json_decode('{"1":1,"2":2}'); //need not typecast but doesnt break even if u typecast

foreach ($a as $k=>$v){
    echo $v;
}

#5


1  

I met the same problem recently.

我最近遇到了同样的问题。

$obj = new stdClass();
$obj->{'0'} = "test";

$array = (array)$obj;
foreach ($array as $key => $value) {
   $array[$key] = strtoupper($value);
}
var_dump($array);

This code outputs :

这段代码输出:

array(2) {
  ["0"]=>
  string(4) "test"
  [0]=>
  string(4) "TEST"
}

Found that when debugging a method to convert recursivly objects to array, I've been mad.

发现当调试一种方法将递归的对象转换为数组时,我已经疯了。

#6


1  

I had the same problem (but with array_intersect_key).

我也遇到了同样的问题(但使用array_intersect_key)。

Here is my solution:

这是我的解决方案:

$array = array_combine(array_keys($array), $array);