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
    ]
  }
]
2

1 Answer 1

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]));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.