PHP`json_encode`错误地将数组转换为{{}

时间:2019-03-20 10:17:43

标签: php arrays json associative-array

我的系统是带有PHP 5.4的centos 7.4

$s='a:91:{s:13:"spotsviewvars";s:7:"1916.74";s:13:"100000T18vars";N;s:17:"100000T18S106vars";s:7:"1746.95";s:17:"100000T18S107vars";s:4:"4.49";s:17:"100000T18S108vars";s:4:"8.29";s:17:"100000T18S109vars";s:4:"4.38";s:17:"100000T18S110vars";s:3:"5.4";s:17:"100000T18S111vars";s:4:"3.88";s:17:"100000T18S112vars";s:4:"3.49";s:17:"100000T18S113vars";s:4:"5.55";s:17:"100000T18S114vars";s:4:"3.58";s:17:"100000T18S115vars";s:3:"5.5";s:17:"100000T18S116vars";s:5:"10.39";s:17:"100000T18S117vars";s:4:"6.52";s:17:"100000T18S118vars";s:4:"6.09";s:17:"100000T18S119vars";s:3:"6.7";s:17:"100000T18S120vars";s:4:"4.18";s:17:"100000T18S121vars";s:5:"14.81";s:17:"100000T18S122vars";s:3:"3.9";s:17:"100000T18S123vars";s:4:"4.93";s:17:"100000T18S124vars";s:4:"4.06";s:17:"100000T18S125vars";s:4:"5.03";s:17:"100000T18S126vars";s:4:"5.73";s:17:"100000T18S127vars";s:4:"3.13";s:17:"100000T18S128vars";s:3:"7.2";s:17:"100000T18S129vars";s:4:"7.03";s:17:"100000T18S130vars";s:4:"3.81";s:17:"100000T18S131vars";s:3:"7.4";s:17:"100000T18S132vars";s:4:"7.82";s:17:"100000T18S133vars";s:4:"3.96";s:13:"100000T19vars";N;s:17:"100000T19S134vars";N;s:17:"100000T19S135vars";N;s:17:"100000T19S136vars";N;s:17:"100000T19S137vars";s:5:"12.54";s:17:"100000T19S138vars";N;s:17:"100000T19S139vars";N;s:17:"100000T19S140vars";N;s:17:"100000T19S141vars";N;s:17:"100000T19S142vars";N;s:17:"100000T19S143vars";N;s:17:"100000T19S144vars";N;s:17:"100000T19S145vars";N;s:17:"100000T19S146vars";N;s:17:"100000T19S147vars";N;s:17:"100000T19S148vars";N;s:17:"100000T19S149vars";N;s:13:"100000T18S106";s:2:"A2";s:13:"100000T18S107";s:2:"A2";s:13:"100000T18S108";s:2:"A2";s:13:"100000T18S109";s:2:"A2";s:13:"100000T18S110";s:2:"A2";s:13:"100000T18S111";s:2:"A2";s:13:"100000T18S112";s:2:"A2";s:13:"100000T18S113";s:2:"A2";s:13:"100000T18S114";s:2:"A2";s:13:"100000T18S115";s:2:"A2";s:13:"100000T18S116";s:2:"A2";s:13:"100000T18S117";s:2:"A2";s:13:"100000T18S118";s:2:"A1";s:13:"100000T18S119";s:2:"A2";s:13:"100000T18S120";s:2:"A1";s:13:"100000T18S121";s:2:"A1";s:13:"100000T18S122";s:2:"A2";s:13:"100000T18S123";s:2:"A2";s:13:"100000T18S124";s:2:"A2";s:13:"100000T18S125";s:2:"A2";s:13:"100000T18S126";s:2:"A1";s:13:"100000T18S127";s:2:"A2";s:13:"100000T18S128";s:2:"A1";s:13:"100000T18S129";s:2:"A1";s:13:"100000T18S130";s:2:"A2";s:13:"100000T18S131";s:2:"A2";s:13:"100000T18S132";s:2:"A1";s:13:"100000T18S133";s:2:"A2";s:13:"100000T19S134";N;s:13:"100000T19S135";N;s:13:"100000T19S136";N;s:13:"100000T19S137";s:0:"";s:13:"100000T19S138";N;s:13:"100000T19S139";N;s:13:"100000T19S140";N;s:13:"100000T19S141";N;s:13:"100000T19S142";N;s:13:"100000T19S143";N;s:13:"100000T19S144";N;s:13:"100000T19S145";N;s:13:"100000T19S146";N;s:13:"100000T19S147";N;s:13:"100000T19S148";N;s:13:"100000T19S149";N;}';
$s_array=unserialize($s);
var_dump($s_array);
$s_json=json_encode($s_array,JSON_HEX_QUOT|JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS|JSON_UNESCAPED_UNICODE);
var_dump($s_json);

在PHP脚本上运行时,您会注意到奇怪的结果。

$s_arrayarrayjson_encode的结果应类似于[],但结果为{}
有什么问题?

2 个答案:

答案 0 :(得分:2)

您似乎只是对术语差异感到困惑。

在PHP中,“数组”可以是索引数组(其中有一个值序列)或关联数组(其中有name => value对)。

在JSON中,值序列存储在数组中,一组name => value对存储在对象中。

由于数据中有name => value对,因此在将其转换为JSON时会得到一个对象。

为进行比较:

$indexed_array = [ "foo", "bar", "baz" ];
$associative_array = [ "foo" => "A", "bar" => "B", "baz" => "C" ];
print json_encode($indexed_array);
print "\n";
print json_encode($associative_array);

答案 1 :(得分:0)

代码完全可以正常工作。

您所拥有的是一个 associative 数组-即每个条目都有一个名称(例如"spotsviewvars""100000T18vars"而不是数字索引(例如{{ 1}}或0)。实际上是一组键/值对。

因此,为了在以JSON编码时保留该信息,必须将其转换为对象-JSON没有关联数组的概念。同时保留键(例如1)和关联值(例如"spotsviewvars")的唯一可能的JSON表示是一个对象。

如果将其转换为简单数组,则会丢失关键信息,我认为这很重要。每个值都只具有一个数字索引来保存它,而您不知道其原始含义是什么。