将稀疏的 json 对象解码为 php 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2484726/
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
Decode sparse json object to php array
提问by Isaac Sutherland
I can create a sparse php array (or map) using the command:
我可以使用以下命令创建一个稀疏的 php 数组(或映射):
$myarray = array(10=>'hi','test20'=>'howdy');
I want to serialize/deserialize this as JSON. I can serialize it using the command:
我想将其序列化/反序列化为 JSON。我可以使用以下命令对其进行序列化:
$json = json_encode($myarray);
which results in the string {"10":"hi","test20":"howdy"}. However, when I deserialize this and cast it to an array using the command:
这导致字符串{"10":"hi","test20":"howdy"}. 但是,当我反序列化它并使用以下命令将其转换为数组时:
$mynewarray = (array)json_decode($json);
I seem to lose any mappings with keys which were not valid php identifiers. That is, mynewarrayhas mapping 'test20'=>'howdy', but not 10=>'hi'nor '10'=>'hi'.
我似乎丢失了与不是有效 php 标识符的键的任何映射。也就是说,mynewarray具有 mapping 'test20'=>'howdy',但没有10=>'hi'nor '10'=>'hi'。
Is there a way to preserve the numerical keys in a php map when converting to and back from json using the standard json_encode/ json_decodefunctions?
在使用标准json_encode/json_decode函数从 json 转换和返回时,有没有办法在 php 映射中保留数字键?
(I am using PHP Version 5.2.10-2ubuntu6.4.)
(我使用的是 PHP 版本 5.2.10-2ubuntu6.4。)
回答by Chris
json_decodereturns an object of type stdClass by default. You access members as properties (i.e., $result->test20). 10isn't a valid name for a property, which is why you're losing it.
json_decode默认返回一个 stdClass 类型的对象。您可以将成员作为属性(即$result->test20)来访问。10不是财产的有效名称,这就是您丢失它的原因。
Instead of casting to an array, you can pass trueas a second argument to json_decodeto make it return an associative array itself:
您可以将其true作为第二个参数传递给数组,json_decode以使其返回关联数组本身,而不是强制转换为数组:
$mynewarray = json_decode($json, true);
If you do that, $mynewarray[10]will work fine.
如果你这样做,$mynewarray[10]就会工作得很好。
回答by Alan Storm
What version of PHP? On 5.2 the following program/script
什么版本的PHP?在 5.2 以下程序/脚本
$myarray = array(10=>'hi','test20'=>'howdy');
$json = json_encode($myarray);
$mynewarray = (array) json_decode($json);
var_dump($mynewarray);
Outputs
输出
array(2) {
["10"]=>
string(2) "hi"
["test20"]=>
string(5) "howdy"
}
Which doesn't display the behavior you're describing.
这不会显示您所描述的行为。
That said, if your version of PHP is miscasting the JSON, try using get_object_vars on the stdClass object that json_decode returns
也就是说,如果您的 PHP 版本错误地投射了 JSON,请尝试在 json_decode 返回的 stdClass 对象上使用 get_object_vars
get_object_vars(json_decode($json))
That might return better results.
这可能会返回更好的结果。
回答by vava
The problem is in the conversion from object to array.
问题在于从对象到数组的转换。
$a = (array)json_decode('{"10":"hi","test20":"howdy"}');
var_dump($a);
//outputs
array(2) {
["10"]=>
string(2) "hi"
["test20"]=>
string(5) "howdy"
}
See how this array have index "10"? But in PHP, everything that looks like a number gets converted into a number, especially in array indexes. You can't just get a["10"]because it converts "10"into a number and this array does not have such an index.
看看这个数组如何有索引"10"?但是在 PHP 中,所有看起来像数字的东西都会被转换成数字,尤其是在数组索引中。你不能只是a["10"]因为它转换"10"成一个数字而这个数组没有这样的索引。
However, foreachworks.
但是,foreach有效。
foreach ($a as $key => $value) {
var_dump($key);
var_dump($value);
}
//outputs
string(2) "10"
string(2) "hi"
string(6) "test20"
string(5) "howdy"
You can also treat result of json_decode as an object. While you won't be able to do $a->10or $a->"10",
您还可以将 json_decode 的结果视为一个对象。虽然你不能做$a->10或$a->"10",
$a = json_decode('{"10":"hi","test20":"howdy"}');
$b = 10;
var_dump($a->$b);
//outputs
string(2) "hi"
works.
作品。
But most likely, as Chris said, you just want to pass trueas a second argument.
但很可能,正如 Chris 所说,您只想true作为第二个参数传递。
$a = json_decode('{"10":"hi","test20":"howdy"}', true);
var_dump($a[10]);
//outputs
string(2) "hi"

