PH7 icon indicating copy to clipboard operation
PH7 copied to clipboard

Associative array is not encoded into an object using `json_encode`.

Open Maxwell175 opened this issue 7 years ago • 3 comments

Hello,

I found that if an associative array is returned from a foreign function and passed into json_encode, it returns an array instead of an object unless I add JSON_FORCE_OBJECT. This is not the correct behaviour. Associative arrays are supposed to encode into json objects.

Maxwell175 avatar Aug 18 '18 21:08 Maxwell175

The default behavior is to return an array instead of an object unless you force it. That was the default case on 2012 unless the PHP group changed it later.

-----Original Message----- From: MDTech-us-MAN [email protected] To: symisc/PH7 [email protected] Cc: Subscribed [email protected] Sent: Sat, 18 Aug 2018 22:14 Subject: [symisc/PH7] Associative array is not encoded into an object using json_encode. (#26)

Hello,

I found that if an associative array returned from a foreign function and passed into json_encode, it returns an array instead of an object unless I add JSON_FORCE_OBJECT. This is not the correct behaviour.

-- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/symisc/PH7/issues/26

symisc avatar Aug 18 '18 21:08 symisc

This is a link to a php tutorial for this kind of thing: https://www.dyn-web.com/tutorials/php-js/json/array.php#assoc

See this online php sandbox: http://sandbox.onlinephpfunctions.com/code/1d75b0a84813ebb107646563f5935d4aa524f37d

The result is the same as far back as version 5.2.16 (released 16 December 2010), which seems to be the first version that has json_encode.

Maxwell175 avatar Aug 18 '18 21:08 Maxwell175

Here is a sample that shows what JSON_FORCE_OBJECT is supposed to do: http://sandbox.onlinephpfunctions.com/code/e03e7d0c95e0a6528bc63fbc732fabeeed2baae4

Maxwell175 avatar Aug 18 '18 22:08 Maxwell175