1

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
    ]
  }
]
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
Mark Brown
  • 914
  • 1
  • 11
  • 26
  • Well first you read the manual – RiggsFolly Jun 25 '15 at 19:05
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 25 '15 at 20:23

1 Answers1

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]));
meda
  • 45,103
  • 14
  • 92
  • 122