Foreach循环问题和输出

发布于 2024-12-10 09:29:54 字数 2990 浏览 0 评论 0原文

首先,我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

善良天后 2024-12-17 09:29:54

无论变量 $account 是什么,它都会在每次迭代 $metricsArray as $m 时循环访问同一个数组。

Whatever variable $account is, it's looping through the same array every iteration of $metricsArray as $m.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文