JSON从多维数组获取数据 [英] JSON getting data from multidimensional arrays
问题描述
我一直在填充一个图表,其中嵌入了来自多维PHP数组($ result)的数据.当将数组打印为JSON时(使用print json_encode($ result,JSON_NUMERIC_CHECK);),我得到了以下数组结构:
I have been populating a chart embed with data from a multidimensional PHP array ($result). When printing the array to JSON (using print json_encode($result, JSON_NUMERIC_CHECK);) i got the following array structure:
[
{
"name":"Array1",
"data":[
1,
2,
3
]
},
{
"name":"Array2",
"data":[
1,
2,
3
]
}
]
我使用此数组在下面的代码中填充了我的图表.过去这种方法工作得很好,但是在我更改了阵列的设置之后,现在必须进行重新设计.
I used this array to populate my highcharts in the code below. This used to work just fine but after I changed the setup of my array it will now have to be reworked.
$.getJSON("../data.php", {id: escape(tableName)}, function(json) {
chartOptions.chart1.xAxis.categories = json[0]['data'];
chartOptions.chart1.series[0].data = json[1]['data'];
});
在进行一些更改后,我的$ result数组的新设置如下:
The new setup of my $result array after making some changes is the below:
{
"Array1":{
"data":[
"1",
"2",
"3"
]
},
"Array2":{
"data":[
"1",
"2",
"3"
]
}
}
因此,我用来填充Highcharts的代码不再起作用.如果有人可以帮助我理解如何重做$ .getJSON代码,使其与新的数组结构一起使用,我将不胜感激.还是告诉我是否必须坚持旧的阵列设置?谢谢.
As such, the code I used to populate my Highcharts no longer works. I would very much appreciate if anyone can help me understand how I can rework the $.getJSON code such that it would work with the new array structure. Or maybe inform me if I have to stick with the old array setup? Thanks.
推荐答案
据我所知(未经测试),您只需要更改:
From what I can tell (haven't tested), you just need to change:
$.getJSON("../data.php", {id: escape(tableName)}, function(json) {
chartOptions.chart1.xAxis.categories = json[0]['data'];
chartOptions.chart1.series[0].data = json[1]['data'];
});
收件人
$.getJSON("../data.php", {id: escape(tableName)}, function(json) {
chartOptions.chart1.xAxis.categories = json['Array1']['data'];
chartOptions.chart1.series[0].data = json['Array2']['data'];
});
JSON结构的更改从字典数组更改为字典字典,因此您不再通过索引访问它,而是通过键(Array1,Array2)访问它.
The change in the JSON structure changed from an array of dictionaries, to a dictionary of dictionaries, so you no longer access it via index, instead you access it by key (Array1, Array2).
这篇关于JSON从多维数组获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!