Discuss Problem with output of json_decode in the PHP Development forum on Dev Shed. Problem with output of json_decode PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Posts: 49
Time spent in forums: 17 h 27 m 23 sec
Reputation Power: 1
PHP-General - Problem with output of json_decode
I am trying to pass input to a PHP program via AJAX. The resulting array is not what I expected. A sub array gets mangled as can be seen in the test command line program below.
As you can see, decoding and encoding returns a different and erronious string.
I would appreciate any advice on the format of the newgame string or any other comments on this problem.
Posts: 12,690
Time spent in forums: 5 Months 1 Week 4 Days 3 h 43 m 47 sec
Reputation Power: 8969
It's not erroneous: json_decode() with $assoc=true is potentially lossy. Arguably not though because {"0":"pname0"} and ["pname0"] will behave nearly identically in Javascript.
Posts: 9,801
Time spent in forums: 2 Months 3 Weeks 16 h 59 m 20 sec
Reputation Power: 6112
I'm not sure what the problem even is here. The objects are still the same, regardless of what the keys end up being. Since JS doesn't support string keys, the whole exercise is kind of moot.
Encoding and decoding the string you gave us produces the same array:
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002