Foreach循环问题和输出
首先,我对 PHP 和使用的最佳实践确实没有那么丰富的经验,所以请保持友善;)
无论如何,我正在努力将 google 分析输出到 java 表,其中我的 foreach 显示页面点击量和页面浏览量,根本不听我的:/
只是为了澄清,数组没有任何问题,当在常规表中查找值时,它们是正确的。我很确定我的 foreach 有一些东西,也许还有一些循环计数或 ??不知道吗?
Array (
[0] => Array ( [id] => http://www.google.com/analytics/feeds/data?ga:date=20110930&start-date=2011-09-30&end-date=2011-10-15 [updated] => 2011-10-14T17:00:00.001-07:00 [title] => ga:date=20110930 [dimensions] => Array ( [name] => date [value] => 20110930 ) [metrics] => Array ( [0] => Array ( [confidenceInterval] => 0.0 [name] => newVisits [type] => integer [value] => 4 )
[1] => Array ( [id] => http://www.google.com/analytics/feeds/data?date=20111001&start-date=2011-09-30&end-date=2011-10-15 [updated] => 2011-10-14T17:00:00.001-07:00 [title] => ga:date=20111001 [dimensions] => Array ( [name] => date [value] => 20111001 ) [metrics] => Array ( [0] => Array ( [confidenceInterval] => 0.0 [name] => newVisits [type] => integer [value] => 5 ) [1] => Array ( [confidenceInterval] => 0.0 [name] => uniquePageviews [type] => integer [value] => 54 ) ) )
and so on.
现在我的搜寻: 注意:metricsarray => (uniquePageviews + NewVisits),仅针对标题
<?php if (!empty($metricsArray)): ?>
<?php foreach ($metricsArray as $m): ?>
<tr>
<th><?php echo $m ?></th> <!-- To display UniqueHits and Pageviews titles-->
<?php
foreach ($account['Account']['dataPoints'] as $data): ?>
<?php if (!empty($data['metrics'])): ?>
<?php foreach ($data['metrics'] as $key => $val): ?>
<?php if (is_numeric($key)): ?>
<td><?php echo $val['value'] ?></td>
<?php elseif ($key == 'value'): ?>
<td><?php echo $val ?></td>
<?php endif ?>
<?php endforeach ?>
<?php endif ?>
<?php endforeach ?>
</tr>
<?php endforeach ?>
<?php endif ?>
我的问题是这个输出如下:
<tr>
<th>NewVisits</th>
<td>1</td> <!-- Unique visit
<td>7</td> <!-- PageViews
<td>2</td> <!-- Uniwue hit
<td>7</td> <!-- PageViews
+++++
</tr>
<tr>
<th>NUniquePageviews</th>
<td>1</td> <!-- Unique visit
<td>7</td> <!-- PageViews
<td>2</td> <!-- Uniwue hit
<td>7</td> <!-- PageViews
+++++
</tr>
我的问题是,它在两个 TR 中显示完全相同,但 TH 标题文本除外。 它应该仅在 TR 1 中显示 Unique vistis,在 TR2 中显示 Pageviews。
感谢您对此的任何意见……
亲切的问候! -汤姆
First off, I am really not that experienced with PHP, and the best practises to use, so please, be nice ;)
Anyway, I am struggling with a google analytics output to a java table, where my foreach to display pagehits and pageviews, does not listen to me at all :/
Just to clearify, There is nothing wrong with the array, when foreaching values in a regular table, they are correct. I am quite sure there is something with my foreach and perhaps some counting of loops or ?? have no idea?
Array (
[0] => Array ( [id] => http://www.google.com/analytics/feeds/data?ga:date=20110930&start-date=2011-09-30&end-date=2011-10-15 [updated] => 2011-10-14T17:00:00.001-07:00 [title] => ga:date=20110930 [dimensions] => Array ( [name] => date [value] => 20110930 ) [metrics] => Array ( [0] => Array ( [confidenceInterval] => 0.0 [name] => newVisits [type] => integer [value] => 4 )
[1] => Array ( [id] => http://www.google.com/analytics/feeds/data?date=20111001&start-date=2011-09-30&end-date=2011-10-15 [updated] => 2011-10-14T17:00:00.001-07:00 [title] => ga:date=20111001 [dimensions] => Array ( [name] => date [value] => 20111001 ) [metrics] => Array ( [0] => Array ( [confidenceInterval] => 0.0 [name] => newVisits [type] => integer [value] => 5 ) [1] => Array ( [confidenceInterval] => 0.0 [name] => uniquePageviews [type] => integer [value] => 54 ) ) )
and so on.
And now my foreaching:
Note: metricsarray => (uniquePageviews + NewVisits), just for the titles
<?php if (!empty($metricsArray)): ?>
<?php foreach ($metricsArray as $m): ?>
<tr>
<th><?php echo $m ?></th> <!-- To display UniqueHits and Pageviews titles-->
<?php
foreach ($account['Account']['dataPoints'] as $data): ?>
<?php if (!empty($data['metrics'])): ?>
<?php foreach ($data['metrics'] as $key => $val): ?>
<?php if (is_numeric($key)): ?>
<td><?php echo $val['value'] ?></td>
<?php elseif ($key == 'value'): ?>
<td><?php echo $val ?></td>
<?php endif ?>
<?php endforeach ?>
<?php endif ?>
<?php endforeach ?>
</tr>
<?php endforeach ?>
<?php endif ?>
My problem is that this output like this:
<tr>
<th>NewVisits</th>
<td>1</td> <!-- Unique visit
<td>7</td> <!-- PageViews
<td>2</td> <!-- Uniwue hit
<td>7</td> <!-- PageViews
+++++
</tr>
<tr>
<th>NUniquePageviews</th>
<td>1</td> <!-- Unique visit
<td>7</td> <!-- PageViews
<td>2</td> <!-- Uniwue hit
<td>7</td> <!-- PageViews
+++++
</tr>
My problem here, is that it display the exact same in both TR, with the exception of the TH title text.
It should display Unique vistis only in TR 1, and Pageviews in TR2.
Thanks for any input on this..
Kind Regards!
-Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论变量
$account
是什么,它都会在每次迭代$metricsArray as $m
时循环访问同一个数组。Whatever variable
$account
is, it's looping through the same array every iteration of$metricsArray as $m
.