我正在尝试返回示例中的数据,因为在高级游戏中我尝试了很多,但我的数据输出是相当不同的,然后显示wihtin highcart当我尝试pu我的数据图形数据没有显示
任何人都可以帮助我
这里是从高图显示的样本数据
?([
[Date.UTC(2013,5,2),0.7695],
[Date.UTC(2013,5,3),0.7648],
[Date.UTC(2013,5,4),0.7645],
[Date.UTC(2013,5,5),0.7638],
[Date.UTC(2013,5,6),0.7549],
[Date.UTC(2013,5,7),0.7562],
[Date.UTC(2013,5,9),0.7574],
这是我的返回数据
["[Date.UTC(2012,05,18), 14.38],
","
[Date.UTC(2012,05,21), 14.15],
","
[Date.UTC(2012,05,22), 15.11],
","
[Date.UTC(2012,05,23), 14.96],
","
[Date.UTC(2012,05,24), 14.98],
","
[Date.UTC(2012,05,25), 15.05],
","
[Date.UTC(2012,05,28), 14.8],
","
[Date.UTC(2012,05,29), 14.71],
","
[Date.UTC(2012,05,30), 14.72],",
这是我的代码
$chartData = array();
foreach ($data as $key => $value) {
$date = explode('-', $value['date']);
$new_date = $date[0]. "," .$date[1]. "," .$date[2];
$value = $value['closing_rate'];
$datetime = 'Date.'. 'UTC('.$new_date.')';
$chartData[] = "[$datetime, $value]";
}
echo json_encode($chartData, JSON_NUMERIC_CHECK);
答案 0 :(得分:2)
您在第一个块中显示的示例数据包含Javascript代码; Date.UTC()
是对Javascript UTC
类的方法Date
的调用。 JSON不能包含代码,它只包含数据。
在第二个列表中,您将生成一些包含Javascript代码的字符串。 JSON在Javascript中正确解码,但它包含的是一个字符串列表,看起来像第一个列表中的代码。您不需要代码,但需要结果。
Date.UTC()
是Javascript Date
类的静态方法,computes and returns:
返回值
一个数字,表示自
January 1, 1970, 00:00:00
以来给定Date对象的毫秒数,通用时间。
有许多方法可以在PHP中生成相同的值。最简单的方法是让DateTime
班级解决这个问题:
// Use the UTC timezone
$tz = new DateTimeZone('UTC');
// Prepare the data here
$chartData = array();
foreach ($data as $key => $value) {
$date = new DateTime($value['date'], $tz);
// DateTime::format('U') returns the number of seconds since Jan 1, 1970
// multiply by 1000 to get the number of milliseconds
$chartData[] = array(1000 * $date->format('U'), $value['closing_rate']);
}
echo json_encode($chartData, JSON_NUMERIC_CHECK);
<强>备注强>
如果您在PHP 7或更高版本上运行代码,那么您可以使用DateTime::format('v')
来获取毫秒数:
foreach ($data as $key => $value) {
$date = new DateTime($value['date'], $tz);
$chartData[] = array($date->format('v'), $value['closing_rate']);
}