在不使用JSON_NUMERIC_CHECK的情况下从数组中删除引号 [英] remove quote from array without using JSON_NUMERIC_CHECK

查看:201
本文介绍了在不使用JSON_NUMERIC_CHECK的情况下从数组中删除引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用高图绘制散点图.

I am trying to plot a scatter plot using highcharts.

JavaScript代码:

Javascript code:

<script type='text/javascript'>
\$(function () {
var chart;
\$(document).ready(function() {
   chart = new Highcharts.Chart({
        chart: {
            renderTo: '$div_id',
            type: 'scatter',
            zoomType: 'xy',                
            marginRight: 450,
            marginBottom: 75
        },
...
...
       series: [{
             name: 'name1',
             data: $data    
       }]
     });
   }); 
});
</script>";

我的php代码:

$data_array4 =  mysql_query("SELECT * from table", $link);
$ret1 = array();
while($item = mysql_fetch_array($data_array4)) {
    $La = $item['L'];
    $Ge = $item['G'];
    $ret1[] = array($La,$Ge);
}
$data = json_encode($ret1);

我的数据如下:

data: [["0.457","0.02"],["0.464","0.02"],["0.469","0.01"],["0.469","0.01"]]

由于双引号,所以图表未显示. 如何在不使用$ data = json_encode($ ret1,JSON_NUMERIC_CHECK)的情况下删除它们,我在PHP 5.2上无法使用该参数.

The chart didn't show up because the double quotes. How can I remove them without using $data = json_encode($ret1,JSON_NUMERIC_CHECK), I am on PHP 5.2, cannot use that parameter.

谢谢!

推荐答案

似乎您不是仅仅从注释中弄清楚.但是,当然,您需要将两个字符串都转换为实数:

It seems you didn't figure it out from the comments just. But of course you need to convert both strings to real numbers:

$La = (float) $item['L'];
$Ge = (float) $item['G'];
$ret1[] = array($La,$Ge);

否则,结果列表将包含一个字符串和一个数字,从而生成JSON对象/映射而不是列表.

Else the resulting list will contain one string and one number, resulting in a JSON object/map instead of a list.

您的下一个问题:是的,浮点数不够精确,并不总是与原始字符串表示形式一样短.

Your next question: Yes, floats are inprecise and not always as short as the original string representation.

这篇关于在不使用JSON_NUMERIC_CHECK的情况下从数组中删除引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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