PHP MySQL and json_encode without quotes around value?

时间:2015-06-25 19:00:30

标签: php mysql json

I've got a MySql table I'm running a query on <?php // Make a MySQL Connection mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("GPS") or die(mysql_error()); // Get all the data from the "example" table $myquery = "SELECT * FROM GPSCoords"; $query = mysql_query($myquery) or die(mysql_error()); $data = array(); for ($x =0; $x < mysql_num_rows($query); $x++){ $data[] = mysql_fetch_assoc($query); } echo json_encode($data); ?> The json returned is as such: [ { "Row": "01SS", "Number": "1E", "Latitude": "33.39064308000000", "Longitude": "-121.918467900", "LatLong": "[33.39064308000000,-121.918467900]" } ] How would I go about getting the LatLong value as unquoted? i.e. [ { "Row": "01SS", "Number": "1E", "Latitude": "33.39064308000000", "Longitude": "-121.918467900", "LatLong": [ 33.39064308000000, -121.918467900 ] } ]

1 个答案:

答案 0 :(得分:2)

Force it like this: while ($row = mysql_fetch_assoc($result)) { $row["Latitude"] = floatval($row["Latitude"]); $row["Longitude"] = floatval($row["Longitude"]); $data[] = $row; } as for "LatLong": "[33.39064308000000,-121.918467900]" That's weird , it looks like you stored this JSON array as a string in your Database. You can do this: $latLong = json_decode($row["LatLong"], true); $row["LatLong"] = array(floatval($latLong[0]), floatval($latLong[1]));