开发者

php foreach loop prints out more than expected

开发者 https://www.devze.com 2023-03-15 03:18 出处:网络
The following code prints the same set of information twice, and I\'m not sure why.I\'m fairly certain it has something to do with my foreach loop, but can\'t figure out what\'s funky about it...

The following code prints the same set of information twice, and I'm not sure why. I'm fairly certain it has something to do with my foreach loop, but can't figure out what's funky about it...

This is the $item array output:

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
                        )

                )

        )

)

And this is the code I'm using to print the information. The code below prints the entire output two times:

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 '&l开发者_StackOverflow中文版t;/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>';
    }
  }
}


Your foreach loop is fine. It only looks like it's printing out twice because the comments array or whatever has the same key names. Not to mention the values are similar.


I just tried your code: http://codepad.org/3owMBlqT

It does not print twice (that I see).

Make sure you are not including or requiring that php script twice accidentally on the page.

0

精彩评论

暂无评论...
验证码 换一张
取 消