If I use json_encode() on an array like this:
return json_encode(array( 'foo' => 'bar'));
The return is:
{'foo' : 'bar'}
The key is passed as a literal, and that is tripping up my script. What I really need is:
{ foo : 'bar' }
Does json_encode do that or do I have to strip the quotes out myself with some ugly regex?
When I test this portion of code :
I get :
Which is valid JSON.
(Note these are double-quotes, and not simple-quotes as you posted)
The ouput you are asking for :
is valid Javascript, but is not valid JSON — so
json_encodewill not return that.See json.org for the specification of the JSON format — which is a subset of Javascript, and not Javascript itself.
Instead of “stripping the quotes out myself with some ugly regex“, you should adapt your code, so it accepts valid JSON : this is way better, in my opinion.