PHP JSON将输出数字编码为字符串 [英] PHP JSON encode output number as string

查看:497
本文介绍了PHP JSON将输出数字编码为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP和MySQL输出JSON字符串,但纬度和经度将输出为带有引号的字符串.当我尝试将标记添加到Google地图时,这会导致问题.

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.

这是我的代码:

$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 "}";

这是预期的输出:

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

这是我得到的输出:

{"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.

推荐答案

您也可以尝试

echo json_encode( $results, JSON_NUMERIC_CHECK );

一些参考:

PHP JSON常量

这篇关于PHP JSON将输出数字编码为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆