views:

81

answers:

5

I'm doing some work with a Google Analytics class. I get output as below:

Array
(
    [20090401] => Array
        (
            [ga:pageviews] => 5000
            [ga:visits] => 2500
        )

    [20090402] => Array
        (
            [ga:pageviews] => 5000
            [ga:visits] => 2500
        )

etc. How do I get the data to display in a table with the first column showing the date? ie the key for each array element.

like this:

20090401-----5000-----2500

A: 

I'm not quite sure what you're asking but maybe this would help...

<?php
foreach($array as $key => $value)
{
    echo $key . " => " . $value;
}
?>
John
+3  A: 

Try this:

<?php
foreach ($report as $date=>$item) {
  print($date.'-----'.$item['ga:pageviews'].'-----'.$item['ga:visits']);
}
?>

The piece you were missing was assigning the optional variable for the key in your foreach.

Lucas Oman
I assume you mean <code>print($date.'-----'.$item['ga:pageviews'].'-----'.$item['ga:visits']);</code>
Rohan
Thanks a lot - this solved it!
Rohan
Well, you'd format however you like with HTML or newlines. Sorry, this was just a skeleton example.
Lucas Oman
No, I meant that you had typed something else instead of $date.
Rohan
Oh, yeah, I went back and fixed that when I noticed it. Sorry about that.
Lucas Oman
A: 

Untested, but here's the idea...

foreach ( $report as $item => $data ) {
  echo implode( '-----', array( $item, $data['ga:pageviews'], $data['ga:visits'] ) );
}
Chris
A: 
foreach($array AS $date => $data){
  echo '
<tr>
  <td>'.$date.'</td>
  <td>'.$data['ga:pageviews'].'</td>
  <td>'.$data['ga:visits'].'</td>
</tr>';
}

Check the php documentation about the foreach construct if you did not know about it.

Arkh
A: 

The PHP function array_keys might help you out too: http://us.php.net/manual/en/function.array-keys.php

Bart