将 stdClass 转换为数组 - 键不可访问
casting stdClass to array - keys not accessible
我是 运行 PHP7.0.9,当我这样做时:
$s = json_decode('{"1": "xxx"}');//decode json to stdClass
$a = (array)$s;//cast to array
die(var_dump($a, $a['1'], $a[1], count($a)));
我得到这个结果:
array (size=1)
'1' => string 'xxx' (length=3) //<-- key 1 exists
null // $a['1'] yields null
null // $a[1] doesn't work either
int 1 // count shows there is 1 element in the array
我期待这个结果:
array (size=1)
'1' => string 'xxx' (length=3)
string 'xxx' (length=3) // $a['1'] should work
null
int 1
我的问题:为什么我不能访问 $a['1']
,即使数组的 count
和 var_dump
都告诉我这个键存在?这是 PHP 中的错误还是某种功能?
数组到对象
If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values, with the exception of numeric keys which will be inaccessible unless iterated.
对象到数组
类似的 issues/quirks 在转换时会出现:(documentation)
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; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour:
基本上,具有数字属性的对象可以转换为数组,并且可以对其进行迭代,但是无法(直接)访问键。这是一个众所周知的怪癖。您可以通过使用 json_decode($string, true);
立即转换为数组来绕过它, 或 使用辅助循环来 "reconstruct" 数组:
$reconstructed = [];
foreach ((array) $obj as $k => $v) {
$reconstructed[$k] = $v;
}
不清楚这是错误还是功能。当我第一次遇到这种行为时,我称它为错误。鉴于它已被记录在案并且是一个已知的怪癖,我现在要说两者都不是。它不是真正的错误,因为它是已知的、理解的和记录的,但它几乎不是一个 特性 。这只是大多数语言都有的那些乱七八糟的怪癖之一。
你必须绕过它,忍受它,避免它,并处理它。鉴于 PHP 邮件列表中有很多关于此的错误报告,并且已将其添加到文档中,这可能不太可能很快得到修复。
我是 运行 PHP7.0.9,当我这样做时:
$s = json_decode('{"1": "xxx"}');//decode json to stdClass
$a = (array)$s;//cast to array
die(var_dump($a, $a['1'], $a[1], count($a)));
我得到这个结果:
array (size=1)
'1' => string 'xxx' (length=3) //<-- key 1 exists
null // $a['1'] yields null
null // $a[1] doesn't work either
int 1 // count shows there is 1 element in the array
我期待这个结果:
array (size=1)
'1' => string 'xxx' (length=3)
string 'xxx' (length=3) // $a['1'] should work
null
int 1
我的问题:为什么我不能访问 $a['1']
,即使数组的 count
和 var_dump
都告诉我这个键存在?这是 PHP 中的错误还是某种功能?
数组到对象
If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values, with the exception of numeric keys which will be inaccessible unless iterated.
对象到数组
类似的 issues/quirks 在转换时会出现:(documentation)
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; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour:
基本上,具有数字属性的对象可以转换为数组,并且可以对其进行迭代,但是无法(直接)访问键。这是一个众所周知的怪癖。您可以通过使用 json_decode($string, true);
立即转换为数组来绕过它, 或 使用辅助循环来 "reconstruct" 数组:
$reconstructed = [];
foreach ((array) $obj as $k => $v) {
$reconstructed[$k] = $v;
}
不清楚这是错误还是功能。当我第一次遇到这种行为时,我称它为错误。鉴于它已被记录在案并且是一个已知的怪癖,我现在要说两者都不是。它不是真正的错误,因为它是已知的、理解的和记录的,但它几乎不是一个 特性 。这只是大多数语言都有的那些乱七八糟的怪癖之一。
你必须绕过它,忍受它,避免它,并处理它。鉴于 PHP 邮件列表中有很多关于此的错误报告,并且已将其添加到文档中,这可能不太可能很快得到修复。