[英]Multidimensional array json_encoded
I try to return a multidimensional array of dates and revenues each date as JSon array in order to display a chart in my panel dashboard. 我试图将日期和收入的多维数组作为JSon数组返回,以便在面板仪表板上显示图表。 Unfortunately the chart makes some problem, when I don't hardcode my JSon Array, thus I am debugging it right now and I wonder why my json_encoded array looks so strange. 不幸的是,当我不对JSon数组进行硬编码时,图表出现了一些问题,因此我现在正在对其进行调试,我想知道为什么我的json_encoded数组看起来如此奇怪。
This is my Panel, which draws the chart and requests the data from data.php: 这是我的面板,它绘制图表并从data.php请求数据:
jQuery.getJSON("data.php", { request: "revenues" }, function (result) {
var revenues = result["amount"];
alert(revenues);
$(".monthly-sales").sparkline(revenues, {
type: 'bar',
barColor: '#ff4e50',
height: '55px',
width: '100%',
barWidth: 8,
barSpacing: 1
});
});
The data.php: data.php:
elseif($request == "revenues"){
// Output Revenues per day from nexus store
$revenues = array();
$revenues["dates"] = array();
$revenues["amount"] = array();
$sql = "select i_total as revenue, date(FROM_UNIXTIME(i_date)) as date FROM nexus_invoices WHERE i_status='paid' GROUP BY date(FROM_UNIXTIME(i_date)) ORDER BY i_date DESC LIMIT 0,10";
$rows = $db->query($sql);
foreach($rows as $row){
$revenues["dates"][] = $row['date'];
$revenues["amount"][] = floatval($row['revenue']);
}
//$revenues["amount"] = [1,5,5.5,5.4,5.8,6,8,9,13,12,10,11.5,9,8,5,8,9];
echo json_encode($revenues);
}
The JSon result of this code is how I expect it: 这段代码的JSon结果是我所期望的:
{
"dates":[
"2015-06-13","2015-06-12","2015-06-10","2015-06-09","2015-06-07",
"2015-06-06","2015-06-05","2015-06-04","2015-06-02","2015-05-31"
],
"amount":[8,22,8,8,22,8,8,8,8,8]
}
Unfortunately this doesn't work for an unknown reason. 不幸的是,由于未知原因,这行不通。 To debug this further I have uncommented the line which hardcodes the $revenues["amount"]
and commented the second line of the foreach. 为了进一步调试它,我取消了对$revenues["amount"]
进行硬编码的行的注释,并注释了foreach的第二行。 Then it worked fine. 然后工作正常。 The result of this json_encoded echo is this: 此json_encoded回显的结果是这样的:
{
"dates":[
"2015-06-13","2015-06-12","2015-06-10","2015-06-09","2015-06-07",
"2015-06-06","2015-06-05","2015-06-04","2015-06-02","2015-05-31"
],
"amount":[1,5,5.5,5.4,5.8,6,8,9,13,12,10,11.5,9,8,5,8,9]
}
How it looks like when I hardcode the array: 硬编码数组时的外观:
How it looks like when I fill the array from my database: 从数据库中填充数组时的外观:
I think your problem is not in the json array rather the sparkline plugin. 我认为您的问题不在json数组中,而是在sparkline插件中。
try adding this when you display the DB data: chartRangeMin:5
在显示数据库数据时尝试添加以下内容: chartRangeMin:5
$(".monthly-sales").sparkline(revenues, {
type: 'bar',
barColor: '#ff4e50',
height: '55px',
width: '100%',
barWidth: 8,
barSpacing: 1,
chartRangeMin:5
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.