Reputation: 2443
My system is centos 7.4 with PHP 5.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);
You will notice strange result when run above PHP script.
$s_array
is array
,json_encode
result should be something like []
,but result is {}
.
What's the problem?
Upvotes: 0
Views: 84
Reputation: 943561
You appear to just have been confused by differences in terminology.
In PHP an "array" can either be an indexed array (in which you have a sequence of values) or an associative array (in which you have name => value
pairs).
In JSON a sequence of values is stored in an array, and a set of name => value
pairs is stored in an object.
Since you have name => value
pairs in your data, you get an object when converting it to JSON.
For comparison:
$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);
Upvotes: 2
Reputation: 61904
The code is working exactly as it should.
What you've got there is an associative array - i.e. each entry has a name (like "spotsviewvars"
, or "100000T18vars"
rather than a numeric index (like 0
or 1
). It's effectively a set of key/value pairs.
So in order to preserve that information when encoding as JSON, it has to be turned into an object instead - JSON has no concept of associative arrays. The only possible JSON representation which keeps both the key (e.g. "spotsviewvars"
) and the associated value (e.g. "1916.74"
) is an object.
If you turned it into a plain array you'd lose the key information, which I would assume is important. Every value would just have a numeric index to hold it instead, and you wouldn't know what its original meaning was.
Upvotes: 0