php 不能使用 stdClass 类型的对象作为数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6815520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot use object of type stdClass as array?
提问by Dail
I get a strange error using json_decode()
. It decode correctly the data (I saw it using print_r
), but when I try to access to info inside the array I get:
我在使用时遇到一个奇怪的错误json_decode()
。它正确解码数据(我看到它使用print_r
),但是当我尝试访问数组内的信息时,我得到:
Fatal error: Cannot use object of type stdClass as array in
C:\Users\Dail\software\abs.php on line 108
I only tried to do: $result['context']
where $result
has the data returned by json_decode()
我只是试图做:$result['context']
哪里$result
有返回的数据json_decode()
How can I read values inside this array?
如何读取此数组中的值?
回答by Jon
Use the second parameter of json_decode
to make it return an array:
使用的第二个参数json_decode
使其返回一个数组:
$result = json_decode($data, true);
回答by svens
The function json_decode()
returns an object by default.
该函数json_decode()
默认返回一个对象。
You can access the data like this:
您可以像这样访问数据:
var_dump($result->context);
If you have identifiers like from-date
(the hyphen would cause a PHP error when using the above method) you have to write:
如果您有类似from-date
的标识符(使用上述方法时连字符会导致 PHP 错误),您必须编写:
var_dump($result->{'from-date'});
If you want an array you can do something like this:
如果你想要一个数组,你可以这样做:
$result = json_decode($json, true);
Or cast the object to an array:
或者将对象强制转换为数组:
$result = (array) json_decode($json);
回答by JiNexus
You must access it using ->
since its an object.
您必须使用它来访问它,->
因为它是一个对象。
Change your code from:
更改您的代码:
$result['context'];
To:
到:
$result->context;
回答by Alexey Lysenko
Have same problem today, solved like this:
今天遇到同样的问题,解决方法如下:
If you call json_decode($somestring)
you will get an Object and you need to access like $object->key
, but if u call json_decode($somestring, true)
you will get an dictionary and can access like $array['key']
如果你打电话json_decode($somestring)
你会得到一个对象,你需要访问 like $object->key
,但是如果你打电话json_decode($somestring, true)
你会得到一个字典并且可以访问 like$array['key']
回答by Sander Marechal
Use true
as the second parameter to json_decode
. This will decode the json into an associative array instead of stdObject
instances:
使用true
作为第二个参数json_decode
。这会将 json 解码为关联数组而不是stdObject
实例:
$my_array = json_decode($my_json, true);
See the documentationfor more details.
有关更多详细信息,请参阅文档。
回答by Wesley van Opdorp
It's not an array, it's an object of type stdClass.
它不是一个数组,它是一个 stdClass 类型的对象。
You can access it like this:
您可以像这样访问它:
echo $oResult->context;
More info here: What is stdClass in PHP?
这里有更多信息:PHP 中的 stdClass 是什么?
回答by Panda
As the Php Manualsay,
正如PHP 手册所说,
print_r — Prints human-readable information about a variable
print_r — 打印关于变量的人类可读信息
When we use json_decode();
, we get an object of type stdClass as return type.
The arguments, which are to be passed inside of print_r()
should either be an array or a string. Hence, we cannot pass an object inside of print_r()
. I found 2 ways to deal with this.
当我们使用时json_decode();
,我们得到一个 stdClass 类型的对象作为返回类型。要在其中传递的参数print_r()
应该是数组或字符串。因此,我们不能在print_r()
. 我找到了 2 种方法来处理这个问题。
Cast the object to array.
This can be achieved as follows.$a = (array)$object;
By accessing the key of the Object
As mentioned earlier, when you usejson_decode();
function, it returns an Object of stdClass. you can access the elements of the object with the help of->
Operator.$value = $object->key;
将对象强制转换为数组。
这可以如下实现。$a = (array)$object;
通过访问 Object 的 key
如前所述,当你使用json_decode();
function 时,它返回一个 stdClass 的 Object。您可以在->
Operator的帮助下访问对象的元素。$value = $object->key;
One, can also use multiple keys to extract the sub elements incase if the object has nested arrays.
一,如果对象有嵌套数组,也可以使用多个键来提取子元素。
$value = $object->key1->key2->key3...;
Their are other options to print_r()
as well, like var_dump();
and var_export();
他们还有其他选择print_r()
,例如var_dump();
和var_export();
P.S: Also, If you set the second parameter of the json_decode();
to true
, it will automatically convert the object to an array();
Here are some references:
http://php.net/manual/en/function.print-r.php
http://php.net/manual/en/function.var-dump.php
http://php.net/manual/en/function.var-export.php
PS:另外,如果你设置json_decode();
to的第二个参数true
,它会自动将对象转换为 anarray();
这里有一些参考:http:
//php.net/manual/en/function.print-r.php
http:// php.net/manual/en/function.var-dump.php
http://php.net/manual/en/function.var-export.php
回答by infomasud
To get an array as result from a json string you should set second param as boolean true.
要从 json 字符串获取数组作为结果,您应该将第二个参数设置为布尔值 true。
$result = json_decode($json_string, true);
$context = $result['context'];
Otherwise $result will be an std object. but you can access values as object.
否则 $result 将是一个 std 对象。但是您可以将值作为对象访问。
$result = json_decode($json_string);
$context = $result->context;
回答by Arnab
You can convert stdClass object to array like:
您可以将 stdClass 对象转换为数组,如:
$array = (array)$stdClass;
回答by Midas Mtileni
When you try to access it as $result['context']
, you treating it as an array, the error it's telling you that you are actually dealing with an object, then you should access it as $result->context
当您尝试以 as 访问它时$result['context']
,您将其视为一个数组,它告诉您实际上正在处理一个对象的错误,那么您应该将其访问为$result->context