开发者

php function json_encode different between verion 5.3X and 5.1.6

开发者 https://www.devze.com 2023-03-27 01:01 出处:网络
I find that json_encode in php version 5.1.6 will not return empty key for example var_dump(json_encode(array(\"\"=>\"value\")));

I find that json_encode in php version 5.1.6 will not return empty key

for example

  1. var_dump(json_encode(array(""=>"value")));
  2. var_dump(json_encode(array(""=>"value1", "key2"=>"value2")));

Expected result:

  1. string(15) "{"":"value"}"
  2. string(17) "{"":"value1", "key2":"value2"}"
开发者_运维百科

Actual result:

  1. string(2) "{}"
  2. string(17) "{"key2":"value2"}"

however in 5.3X

Actual result:

  1. string(15) "{"":"value"}"
  2. string(17) "{"":"value1", "key2":"value2"}"

My question is beside above effect any other difference on json_encode between php 5.3x and 5.1.6


The function json_encodeDocs is part of PHP since version 5.2.0. If you take a look into the manual you will notice a section called Changelog. It documents that the function changed over time and that flags have been introduced to control the json string output.

It's highly likely that the output has changed over time as well and you might need to use additional parameters to better control the expected behavior. Additionally there are some undocumented flags for that function as well.

If you really need to learn about each differences for the output, you need to finally look into the source-code of that function according to version. It's written in C. PHP is open source software, which means, there is nothing hidden, so you can check about any change between versions.


There is no json_encode in PHP before 5.2.1.

You could write you own as a failover:

if (!function_exists('json_encode')) {
    function json_encode($data)
    {
        // your code that parses to json
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消