user192344
user192344

Reputation: 1396

php function json_encode different between verion 5.3X and 5.1.6

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

Upvotes: 1

Views: 821

Answers (2)

hakre
hakre

Reputation: 198204

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.

Upvotes: 1

PeeHaa
PeeHaa

Reputation: 72709

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
    }
}

Upvotes: 0

Related Questions