我正在尝试获得最佳实践来操作我的数组以获取类似于此格式的 json(更好地使用图表)
{
"serieMonth":["Aug-12","Sep-12","Oct-12","Nov-12","Dec-12","Jan-13","Feb-13"],
"serieCA":[4214,10119,13325,12818,7177,20628,7664],
"serieAdwordsCA":[0,0,0,0,0,310,332],
"serieBooking":[10,28,46,34,17,51,16],
"serieAdwords":[0,0,0,0,0,1,1],
"serieTotalBooking":[10,28,46,34,17,52,17],
"serieCartRepartition":[421,361,290,377,422,397,451],
"serieTotalCart":[421,361,290,377,422,397,451]
}
实际上我的输出看起来像这样:
[
{"date_year":"2012","date_month":"08","ad_cost":"0.0","ad_clicks":"0"},
{"date_year":"2012","date_month":"09","ad_cost":"0.0","ad_clicks":"0"},
{"date_year":"2012","date_month":"10","ad_cost":"0.0","ad_clicks":"0"},
{"date_year":"2012","date_month":"11","ad_cost":"0.0","ad_clicks":"0"},
{"date_year":"2012","date_month":"12","ad_cost":"44.9","ad_clicks":"43"},
{"date_year":"2013","date_month":"01","ad_cost":"297.56","ad_clicks":"462"},
{"date_year":"2013","date_month":"02","ad_cost":"82.5","ad_clicks":"103"}
]
我正在使用 javascript 来更改它:
var xAxisLabels = new Array(),
adClicks = new Array(),
adCost = new Array();
$.each(data, function(i,v) {
xAxisLabels.push(v["date_month"]+'/'+v["date_year"]);
adCost.push(parseInt(v["ad_cost"]));
adClicks.push(parseInt(v["ad_clicks"]));
});
我正在寻找在 php 中执行此操作的最佳方法,因为我通过 google api 获取此数据,这是我的 php。
// dimensions
$dimensions = 'ga:year,ga:month';
$_params[] = 'date_year';
$_params[] = 'date_month';
// metrics
$metrics = 'ga:adCost,ga:adClicks';
$_params[] = 'ad_cost';
$_params[] = 'ad_clicks';
$response = $service->data_ga->get('ga:' . $projectId, $from, $to, $metrics, array('dimensions' => $dimensions));
$analyticsStats = array();
foreach ($response['rows'] as $row) {
$dataRow = array();
foreach ($_params as $colNr => $column) {
$dataRow[$column] = $row[$colNr];
}
array_push($analyticsStats, $dataRow);
}