title: PHP Array and Google Analytics V3 API
body: <p>I am using the Google Analytics V3 PHP OAuht API. When using Simple.php in Google Analytics API Example the data are returned as PHP arrays. I am using the following call to get a more detailed answer to some specific data. It works fine.</p>
  
  <pre><code>$ids = "ga:xxxxxx";
  $start_date = "2011-01-01";
  $end_date = "2011-11-30";
  $metrics = "ga:visits,ga:pageviews";
  $dimensions = "ga:browser";
  $optParams = array('dimensions' =&gt; $dimensions);
  $data = $service-&gt;data_ga-&gt;get($ids,$start_date,$end_date,$metrics,$optParams); 
  </code></pre>
  
  <p>Output of the Array is</p>
  
  <pre><code>       Data
  
  Array
  (
      [kind] =&gt; analytics#gaData
      [id] =&gt; https://www.googleapis.com/analytics/v3/data/ga?ids=ga:xxxxxxx&amp;dimensions=ga:browser&amp;metrics=ga:visits,ga:pageviews&amp;start-date=2011-01-01&amp;end-date=2011-11-30&amp;start-index=1&amp;max-results=1000
      [query] =&gt; Array
          (
              [start-date] =&gt; 2011-01-01
              [end-date] =&gt; 2011-11-30
              [ids] =&gt; ga:xxxxxxxx
              [dimensions] =&gt; ga:browser
              [metrics] =&gt; Array
                  (
                      [0] =&gt; ga:visits
                      [1] =&gt; ga:pageviews
                  )
  
              [start-index] =&gt; 1
              [max-results] =&gt; 1000
          )
  
      [itemsPerPage] =&gt; 1000
      [totalResults] =&gt; 220
      [selfLink] =&gt; https://www.googleapis.com/analytics/v3/data/ga?ids=ga:xxxxx&amp;dimensions=ga:browser&amp;metrics=ga:visits,ga:pageviews&amp;start-date=2011-01-01&amp;end-date=2011-11-30&amp;start-index=1&amp;max-results=1000
      [profileInfo] =&gt; Array
          (
              [profileId] =&gt; xxxxx
              [accountId] =&gt; xxxxx
              [webPropertyId] =&gt; UA-xxxxxx-x
              [internalWebPropertyId] =&gt; xxxxxxxxxx
              [profileName] =&gt; xxxxx.com
              [tableId] =&gt; ga:xxxxxxxx
          )
  
      [containsSampledData] =&gt; 
      [columnHeaders] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [name] =&gt; ga:browser
                      [columnType] =&gt; DIMENSION
                      [dataType] =&gt; STRING
                  )
  
              [1] =&gt; Array
                  (
                      [name] =&gt; ga:visits
                      [columnType] =&gt; METRIC
                      [dataType] =&gt; INTEGER
                  )
  
              [2] =&gt; Array
                  (
                      [name] =&gt; ga:pageviews
                      [columnType] =&gt; METRIC
                      [dataType] =&gt; INTEGER
                  )
  
          )
  
      [totalsForAllResults] =&gt; Array
          (
              [ga:visits] =&gt; 36197
              [ga:pageviews] =&gt; 123000
          )
  
      [rows] =&gt; Array
          (
              [0] =&gt; Array
                  (
                      [0] =&gt; (not set)
                      [1] =&gt; 459
                      [2] =&gt; 1237
                  )
  
              [1] =&gt; Array
                  (
                      [0] =&gt; 12345
                      [1] =&gt; 3
                      [2] =&gt; 3
                  )
  
              [2] =&gt; Array
                  (
                      [0] =&gt; 440955
                      [1] =&gt; 1
                      [2] =&gt; 1
                  )
  
              [3] =&gt; Array
                  (
                      [0] =&gt; Alexa Toolbar
                      [1] =&gt; 1
                      [2] =&gt; 1
                  )
  
              [4] =&gt; Array
                  (
                      [0] =&gt; Android Browser
                      [1] =&gt; 4177
                      [2] =&gt; 9896
                  )
  
      ....
  
      The [Rows] Array has 219 entries.
  </code></pre>
  
  <p>Now the problem. I have spent the last week trying to parse this into an HTML table or anything that looks presentable. I have come close, but it seems this multi-dimensional array is beyond what I am able to handle. I am also trying to keep the solution flexible enough to handle more metrics or dimensions if they are added as well. I am self-taught PHP, so maybe I am missing a function or two that could make this easier. Thanks again for any hints, tips of ideas to make this work.</p>
  
  <hr>
  
  <p>I got a bit further, but I does not fully format the way I want...maybe someone can see where I went wrong</p>
  
  <pre><code>$output = $service-&gt;data_ga-&gt;get($ids,$start_date,$end_date,$metrics,$optParams);
  echo'&lt;table style="text-align: left; width: 100%;" border="1" cellpadding="2"
  cellspacing="2"&gt;
  &lt;tbody&gt;&lt;tr&gt;';
  foreach ($output['columnHeaders'] as $header) {
  print "&lt;td&gt;";
  printf('%25s', $header['name']);
  print "&lt;/td&gt;";
  }
  print "&lt;/tr&gt;";
  foreach ($output['rows'] as $row) {
  print "&lt;td&gt;";
  foreach ($row as $column)
  printf('%25s', $column);
  print "&lt;/td&gt;";
  }
  print "\n";
  echo'
  &lt;/tbody&gt;
  &lt;/table&gt;';
  </code></pre>
  
  <p>I still can't seem to get the rows to display right.</p>