7

I am trying to output a JSON string using PHP and MySQL but the latitude and longitude is outputting as a string with quotes around the values. This causes an issue when I am trying to add the markers to a google map.

Here is my code:

$sql = mysql_query('SELECT * FROM markers WHERE address !=""');
$results = array();
while($row = mysql_fetch_array($sql))
{
   $results[] = array(
      'latitude' =>$row['lat'],
      'longitude' => $row['lng'],
      'address' => $row['address'],
      'project_ID' => $row['project_ID'],
      'marker_id' => $row['marker_id']
   );
}
$json = json_encode($results);

echo "{\"markers\":";
echo $json;
echo "}";

Here is the expected output:

{"markers":[{"latitude":0.000000,"longitude":0.000000,"address":"2234 2nd Ave, Seattle, WA","project_ID":"7","marker_id":"21"}]}

Here is the output that I am getting:

{"markers":[{"latitude":"0.000000","longitude":"0.000000","address":"2234 2nd Ave, Seattle, WA","project_ID":"7","marker_id":"21"}]}

Notice the quotes around the latitude and longitude values.

4 Answers 4

17

You could also try

echo json_encode( $results, JSON_NUMERIC_CHECK );

Some reference:

PHP JSON constants

Sign up to request clarification or add additional context in comments.

2 Comments

these JSON_* constants are quite heavy in requirements :)
Note that this only works for PHP >=5.3. If you need to support older versions (a number of sites run PHP 5.2 as of writing this), @Allen Chak's response is quite good
2
$results[] = array(
  'latitude' => (float)$row['lat'],
  'longitude' => (float)$row['lng'],
  'address' => $row['address'],
  'project_ID' => $row['project_ID'],
  'marker_id' => $row['marker_id']
);

latitude AND longitude should be a float number, but you could try this

1 Comment

I like this approach. It only casts the values that need to be.
1

Here's something you may not know: when retrieving data from MySQL in PHP, you will always get strings back, no matter what the actual value is. A simple type casting to float should fix the problem however:

$sql = mysql_query('SELECT * FROM markers WHERE address !=""');
$results = array();
while($row = mysql_fetch_array($sql))
{
   $results[] = array(
      'latitude' => (float) $row['lat'],
      'longitude' => (float) $row['lng'],
      'address' => $row['address'],
      'project_ID' => $row['project_ID'],
      'marker_id' => $row['marker_id']
   );
}
$json = json_encode($results);

echo "{\"markers\":";
echo $json;
echo "}";

Or alternatively, because floats may not be accurate enough to store lat/long coordinates, do store them as strings and remove the quotes on the fly using string manipulation functions such as str_replace.

Comments

0

Use javascripts parseInt() on your client side to turn the string values into integers where necessary:

typeof parseInt("56") # returns number
typeof "56" # returns string

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.