在1个Json对象中加载2个php变量,以便在图形中使用

时间:2013-11-25 19:51:04

标签: javascript php json

我想为我的图表加载两种类型的数据。我有2个php变量:

$ rows和$ rows1

我目前有以下jsone编码whitch工作正常:

echo json_encode(array("className" => ".main.l1","data" => $rows));

但现在我想添加第二组数据。它应该具有类名.main.l2才能工作。

我尝试了类似的东西,但它似乎不起作用:( className被覆盖)

echo json_encode(array("className" => ".main.l1","data" => $rows,"className" => ".main.l2","data" => $rows1));

有谁知道如何阻止.main.l2重写.main.l2?

输出必须如下所示:

{"className":".main.l1","data":[{"x":"2013-11-17","y":4831642},{"x":"2013-11-18","y":6039726},{"x":"2013-11-19","y":6161756},{"x":"2013-11-20","y":5982313},{"x":"2013-11-21","y":5916902},{"x":"2013-11-22","y":5300133},{"x":"2013-11-23","y":4801745}]},{"className":".main.l2","data":[{"x":"2013-11-17","y":4831642},{"x":"2013-11-18","y":6039726},{"x":"2013-11-19","y":6161756},{"x":"2013-11-20","y":5982313},{"x":"2013-11-21","y":5916902},{"x":"2013-11-22","y":5300133},{"x":"2013-11-23","y":4801745}]}

任何人都可以帮我这个吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您需要一个多维数组。像这样:

$array = array(
    array("className" => ".main.l1","data" => $rows),
    array("className" => ".main.l2","data" => $rows1)
);
echo json_encode($array);