开发者

Make JSON array Flot compatible

I have some PHP code that outputs 开发者_开发知识库a JSON array when $.getJSON is called:

.
.
.
$data = array_combine($date_dispatched,$amount);

echo json_encode($data);

This is collected by:

$.getJSON('statistics_get.php',function(output) {
            $.plot($("#graph_1"), [{
                        data: output,
                        lines: {show:true}
                        }]
                    );
});

Now, using firebug, the response I get back is of the form:

{"0":"0","1296658458000":"566","1296725534000":"789","1297072385000":"890","1297072388000":"435"}

Flot is not processing this data. I believe Flot needs it to be in the form [[1,2],[4,5],[6,9]] etc. So my question is, what is the best way of getting this JSON array into the correct form for flot to read and produce a graph.


If you look at example two here: http://php.net/manual/en/function.json-encode.php you will see that you can have json_encode return an array instead of an associative object Would that not be the solution rather than

var flotArr = []
var cnt=0;
for (var o in data) flotArr[cnt++]=[o,data[o]]


Don't use array_combine, since it doesn't do what you need. Doing "zip" in PHP is a little bit unpleasant, but you should be able to manage with:

function make_pair($date, $amount) {
    return array($date, $amount);
}

$data = array_map('make_pair', $date_dispatched, $amount);

And then JSON-encode that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜