Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6082533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:18:00+00:00 2026-05-23T11:18:00+00:00

The following code prints the same set of information twice, and I’m not sure

  • 0

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 '</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>';
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:18:01+00:00Added an answer on May 23, 2026 at 11:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code prints out 10 . How can I make it print out
The following code prints (when invoking MyMethod): 0 0 0 1 I would expect
The following code prints null once. class MyClass { private static MyClass myClass =
Why is the following code prints xxY? Shouldn't local variables live in the scope
The following java code prints Hello World on my co-worker's computer: public void testJDBC()
if I compile (under G++) and run the following code it prints Foo::Foo(int). However
The following code, prints out Derived Base Base But I need every Derived object
I'm wondering why this code section prints out the following: print request.user.has_perm('bug_tracking.is_developer'): + str(request.user.has_perm('bug_tracking.is_developer'))
One of my fellow developer has a code similar to the following snippet class
I've set up application.html.erb to link to a stylesheet using the following code <%=

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.