J'essaie de trouver la meilleure pratique pour manipuler mon tableau afin d'obtenir un json dans un format similaire à celui-ci (mieux pour travailler avec des graphiques).
{
"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]
}
En fait, mon résultat ressemble à ceci :
[
{"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"}
]
Et j'utilise le javascript pour le modifier :
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"]));
});
Je cherche la meilleure façon de le faire en php puisque je reçois ces données par l'api google, voici mon 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);
}