I’m trying to encode a two-dimensional PHP array to JSON with json_encode() and I’m having a problem:
My array looks like this:
$array = array( array( 1325368800, 6898 ) );
When I encode it to JSON using json_encode(), the output looks like this:
'data' : [
'0' : [ '0' : '1325368800', '1' : '6898' ]
]
I would like to get rid of the automatically generated integer keys and have it like this:
'data': [ [ '1325368800', '6898' ] ]
Any tips?
This can only happen if you give the
JSON_FORCE_OBJECTflag tojson_encode(live demo):Make sure the second argument of
json_encodeis not set, or set to a value generated by ORingJSON_*constants together.If, however, the data is an associative array, you can use
array_valuesto get a JSON array instead of a JSON object:By the way, the JSON output you cited is invalid, since it uses square brackets (
[]) for objects, and misses a quote after"0.