php foreach循环打印出来超出预期

时间:2011-06-23 14:20:54

标签: php foreach

以下代码两次打印相同的信息集,我不知道为什么。我相当肯定它与我的foreach循环有关,但无法弄清楚它的时髦之处......

这是$ item数组输出:

Array
(
    [1] => Array
        (
            [title] => Proin pharetra libero.
            [body] => Proin pharetra libero vitae odio ornare a vehicula metus suscipit. Ut vitae magna imperdiet massa aliquet tempor consequat vitae augue. Phasellus cursus sem nec nunc pulvinar eu iaculis nisi dictum. Ut id urna justo. Morbi at massa tellus. Duis ac lacus lobortis lectus sollicitudin viverra
            [created] => June 17, 2011 01:22 PM
        )

    [3] => Array
        (
            [title] => Nam tincidunt eros in odio gravida
            [body] => Nam tincidunt eros in odio gravida sit amet auctor sapien luctus. Duis vel pretium risus. Aliquam erat volutpat. Nunc a neque vitae elit pharetra dapibus. Vestibulum ullamcorper risus scelerisque lorem luctus in accumsan lectus interdum. Maecenas egestas massa eu urna viverra viverra. Suspendisse potenti. Nulla posuere dui non lacus fermentum ut ultricies dui dictum. 
            [created] => June 17, 2011 01:48 PM
            [comment] => Array
                (
                    [5] => Array
                        (
                            [title] => Re: Nam tincidunt eros in odio gravida...
                            [body] => Quisque vulputate odio eget augue porta mollis. Ut venenatis consectetur turpis, nec gravida diam tincidunt eget. Nulla mauris mi, varius eleifend ultricies sit amet, dapibus ac ipsum.
                            [created] => June 17, 2011 01:50 PM
                        )

                    [4] => Array
                        (
                            [title] => Re: Nam tincidunt eros in odio gravida...
                            [body] => Cras nisl velit, lobortis quis aliquet vehicula, ultrices nec sem. Sed imperdiet arcu eget elit lacinia vel ultricies velit consequat. Aliquam varius ornare congue. Curabitur hendrerit dapibus nulla, ut lacinia ante semper a.
                            [created] => June 17, 2011 01:48 PM
                        )

                )

        )

)

这是我用来打印信息的代码。下面的代码打印整个输出两次:

foreach($item as $key => $value) {
    print '<div class="section-item clearfix question">';
    print '<div class="section-inner">';
    print '<div class="title">'.$value['title'].'</div>';
    print '<div class="body">'.$value['body'].'</div>';
    print '</div>';
    print '</div>';

  if(isset($value['comment'])) {

    foreach($value['comment'] as $ak => $av) {
      print '<div class="section-item clearfix comment">';
      print '<div class="section-inner">';
      print '<div class="title">'.$av['title'].'</div>';
      print '<div class="body">'.$av['body'].'</div>';
      print '</div>';
      print '</div>';
    }
  }
}

2 个答案:

答案 0 :(得分:0)

你的foreach循环很好。它看起来只是打印两次,因为comments数组或任何具有相同键名的东西。更不用说价值观是相似的。

答案 1 :(得分:0)

我刚试过你的代码:http://codepad.org/3owMBlqT

它没有打印两次(我看到)。

确保您没有在页面上意外地包含或要求两次php脚本。