I want to use json_encode on a PHP array that I have, then return it to an AJAX call.
Here is the php array called $match_array
Array ( [0] => 1966318353 [1] => 1962510086 [2] => 1962465781 [3] => 1962349187 [4] => 1962281400 [5] => 1962111347 [6] => 1962016291 [7] => 1961983582 [8] => 1961926702 [9] => 1961799894 )
Using json_encode on $match_array
returns the following:
[1966318353,1962510086,1962465781,1962349187,1962281400,1962111347,1962016291,1961983582,1961926702,1961799894]
This gets interpreted by the parser as a non-json object. Any idea why this might be happening?
Thanks!
You can use the JSON_FORCE_OBJECT
flag, try:
json_encode($array, JSON_FORCE_OBJECT);
json_encode
interprets arrays with numbered key values as a JSON array which is a valid JSON string. If you use something like JSON.parse()
, it will give you back an array in JavaScript. The above poster is correct if you are actually looking for an object type. Using JSON_FORCE_OBJECT will force the array to be in object notation like {'0': 1966318353 ... }