我有一个像这样的数组
Array
(
[interswitch] => Array
(
[0] => Array
(
[month] => 8
[transactions] => 5
)
[1] => Array
(
[month] => 9
[transactions] => 1
)
)
[visa] => Array
(
[0] => Array
(
[month] => 8
[transactions] => 9
)
)
)
该数组由SQL查询生成,该查询查询表并计算每个月的记录。其中[月]代表日历月。我想将这些数据用于接受此格式数据的HighCharts
{name: 'Tokyo',data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1,95.6, 54.4]}
如何将我的数组转换为更易读的数组,然后可以通过JSON编码并转换为上述格式 我试过这个
$_monthly_gateway_usage = array(
array(
"name"=>"Interswitch",
"data"=>$this->parse_monthly_usage_data($monthly_gateway_usage['interswitch'])
),
array(
"name"=>"Visa",
"data"=>$this->parse_monthly_usage_data($monthly_gateway_usage['visa'])
)
);
private function parse_monthly_usage_data($usage_data = array()) {
$months = array("1","2","3","4","5","6","7","8","9","10","11","12");
$_d = array();
foreach ($months as $month) {
foreach ($usage_data as $key => $d) {
if($d['month'] == $month)
{
$_d[] = $d['transactions'];
}
else
{
$_d[] = 0;
}
}
}
return $_d;
}
任何帮助将不胜感激
答案 0 :(得分:1)
这是一种较短的形式:循环索引基于月份
$_d = array();
for($i=1;$i<=12;$i++){
foreach($usage_data as $v){
$_d[$i] += ($v['month']==$i) ? $v['transactions'] : 0;
}
}