Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i'm trying to get the best practice to manipulate my array to get a json in a format similar to this one (better to work with charts)

   {
    "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]
    }

Actually my output looks like this :

[
{"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"}
]

And I'm using javascript to change it :

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"]));
});

I'm looking for the best way to do it in php since I get this data by the google api, here is my 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);
}
share|improve this question
    
The "output" does not seem to contain all values required to build the "input". –  Salman A Feb 12 '13 at 12:07
    
Yeah the output is an exemple for the format I would like to get. –  Shipow Feb 12 '13 at 12:13

1 Answer 1

up vote 3 down vote accepted

You can build an array of arrays then add items the the sub-arrays in a loop:

$output = array(
    "serieMonth" => array(),
    "serieCA" => array(),
    "serieAdwordsCA" => array(),
    "serieBooking" => array(),
    "serieAdwords" => array(),
    "serieTotalBooking" => array(),
    "serieCartRepartition" => array(),
    "serieTotalCart" => array()
);
foreach($response["rows"] as $row) {
    $output["serieMonth"][] = date("Y-M", strtotime("{$row['date_year']}-{$row['date_month']}-01"));
    $output["serieCA"][] = $row["ad_cost"];
    $output["serieAdwordsCA"][] = $row["ad_clicks"];
    // etc...
}
echo json_encode($output);
share|improve this answer
    
Thanks for your help –  Shipow Feb 12 '13 at 15:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.