1

Here is my question

when i run the below code which is php with codeigniter framework

    $data= $datatable->result();
    foreach ($data as $graph) {
    $to_graph[] = "Date.UTC(".$graph->year.")," . $graph->ANN ;
    }
    return json_encode($to_graph);

i get the following out put

    ["Date.UTC(2040),934","Date.UTC(2040),1003","Date.UTC(2040),1747","Date.UTC(2040),1336","Date.UTC(2040),973","Date.UTC(2040),912","Date.UTC(2040),1112","Date.UTC(2040),793","Date.UTC(2040),973","Date.UTC(2040),786","Date.UTC(2040),759","Date.UTC(2040),1001","Date.UTC(2040),1221","Date.UTC(2040),1270","Date.UTC(2040),1355","Date.UTC(2040),923","Date.UTC(2040),942","Date.UTC(2040),999","Date.UTC(2040),1128","Date.UTC(2040),908"]

But for highcharts highstock to draw it on the graph it needs to be displayed like this

[Date.UTC(2003),0.8709],
[Date.UTC(2003),0.872],
[Date.UTC(2003,26),0.8714],
[Date.UTC(2003),0.8638],
[Date.UTC(2003),0.8567],
[Date.UTC(2005),0.8536],
[Date.UTC(2005),0.8564],
[Date.UTC(2005),0.8639],
[Date.UTC(2009),0.8538],
[Date.UTC(2009),0.8489],
[Date.UTC(2009),0.8459],
[Date.UTC(20011),0.8521],
[Date.UTC(2011),0.6945]
]);

so can you help me in converting my json out put to that of highstocks without the quotes ""

Thanks

4

3 回答 3

3
$json = json_encode($to_graph);

就这么简单。确保您的数据首先是 UTF-8 编码的。您还需要引号,因为这就是 json 解释字符串的方式。

于 2013-10-30T16:28:22.863 回答
1

通常在 JSON 中你不能使用函数,所以 Date.UTC() 没有意义。我建议删除 Date.UTC,并在 PHP 中准备时间戳。然后在 json_encode() 中使用标志 JSON_NUMERIC_CHECK。结果你会收到 json,它不会在 javascirpt 中被解析,只会被使用。

于 2013-10-31T11:01:19.770 回答
0

转换为 JSON 不是您的问题。这是 PHP 中微不足道的最后一步。您只需要考虑如何按照输入格式的预期将数据变成多维数组。老实说,我看不到您提供的示例数据中的相关性,无法准确了解您需要如何将数据转换为所需的格式(您显示的数据似乎不匹配)。

但是,我猜该方法将类似于:

$data= $datatable->result();
foreach ($data as $graph) {
    $to_graph[] = array("Date.UTC(" . $graph->year . ")", $graph->ANN);
}
$json = json_encode($to_graph);
于 2013-10-30T16:32:17.847 回答