我如何在php脚本中制作样本json?

时间:2013-04-18 20:39:25

标签: php javascript jquery ajax json

我需要制作生成json数据的示例php代码,如下所示:

?(/* AAPL historical OHLC data from the Google Finance API */
[
/* Apr 2006 */
[1145404800000,65.65],
[1145491200000,67.63],
[1145577600000,67.04],
[1145836800000,65.75],
[1145923200000,66.17],
[1146009600000,68.15],
[1146096000000,69.36],
[1146182400000,70.39],
/* May 2006 */
[1146441600000,69.60],
[1146528000000,71.62],
[1146614400000,71.14],
[1146700800000,71.13],
[1146787200000,71.89],
[1147046400000,71.89],
[1147132800000,71.03],
[1147219200000,70.60],
[1147305600000,68.15],
[1147392000000,67.70],
[1147651200000,67.79],
[1147737600000,64.98],
[1147824000000,65.26],
[1147910400000,63.18],
[1147996800000,64.51],
[1148256000000,63.38],
[1148342400000,63.15],
[1148428800000,63.34],
[1148515200000,64.33],
[1148601600000,63.55],
[1148947200000,61.22],
[1149033600000,59.77],
/* Apr 2013 */
[1364774400000,428.91],
[1364860800000,429.79],
[1364947200000,431.99],
[1365033600000,427.72],
[1365120000000,423.20],
[1365379200000,426.21],
[1365465600000,426.98],
[1365552000000,435.69],
[1365638400000,434.33],
[1365724800000,429.80],
[1365984000000,419.85],
[1366070400000,426.24]
]);

*修改

这样的数据:http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=

但没有加载外部json脚本到PHP脚本。只需在一个脚本php中生成json数据。我怎么能这样做?

2 个答案:

答案 0 :(得分:6)

使用数据创建一个多维数组:

$data=array(
array(1145404800000,65.65),
array(1145491200000,67.63),
array(1145577600000,67.04),
array(1145836800000,65.75),
array(1145923200000,66.17),
//...
);

使用json_encode将其转换为json字符串:

echo json_encode($data);

答案 1 :(得分:2)

生成您需要的数据数组,并将其传递给json_encode