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 8129803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:26:32+00:00 2026-06-06T08:26:32+00:00

So, I have this very weird error – Some advanced PHP devs might also

  • 0

So, I have this very weird error – Some advanced PHP devs might also be considered somewhat weird.

In the 2nd foreach statement I have an array “$reply_each” created in that foreach statement and it is supposed to be “array_push()”-ed into another array called “$recent_discussion_each[‘discussion_replies_array’]”. This works while in the 2nd foreach statement. But as soon as the 2nd foreach statement finishes, the “$reply_each” array just vanishes – as if it was out of scope. Just to be sure it wasn’t a scope issue, I initialized $reply_each at the beginning of the function but that didn’t help – it must be something else.

Help, please?

Update: I probably should add that when I do a count() on $recent_discussion_each[‘discussion_replies_array’] inside the 2nd foreach statement, I get the correct value of 1. But when I do an identical count(), but this time outside of the 2nd foreach statement, I get an incorrect value of 0. This is the problem, if it wasn’t clear before.

public function getRecentDiscussions($num_recent_discussions, $course_id) {
    //
    $this->load->database();
    //
    $recent_discussions_array = array();
    //
    // Construct query to fetch recent discussions along with their replies and some
    // basic user info about the authors
    $query_recent_discussions = $this->db->select('course_discussion.id AS course_discussion_id,
                                                   user.f_name,
                                                   user.l_name,
                                                   course_discussion.text,
                                                   course_discussion.posted_datetime,
                                                   course_discussion.is_reply,
                                                   course_discussion.parent_id
                                                  ')
                                         ->join('user', 'course_discussion.owner = user.id')
                                         ->where('course_discussion.course', $course_id)
                                         ->limit($num_recent_discussions)
                                         ->get('course_discussion');
    //
    foreach($query_recent_discussions->result_array() as $row) {
        //
        // Figure out of this comment is a parent or a reply. If it is a parent (meaning is_reply
        // is equal to 0), then treat it like one
        if($row['is_reply'] == '0') {
            //
            $recent_discussion_each = array();
            //
            $recent_discussion_each['discussion_id'] = $row['course_discussion_id'];
            $recent_discussion_each['discussion_owner_f_name'] = $row['f_name'];
            $recent_discussion_each['discussion_owner_l_name'] = $row['l_name'];
            $recent_discussion_each['discussion_body'] = $row['text'];
            $recent_discussion_each['discussion_posted_datetime'] = $row['posted_datetime'];
            $recent_discussion_each['discussion_replies_array'] = array();
            //
            array_push($recent_discussions_array, $recent_discussion_each);
        }
        //
        // Else, it must be a reply since is_reply is not a 0
        else {
            //
            // Look for the parent comment by going through the entire list of comments
            foreach($recent_discussions_array as $potential_parent) {
                //
                // Check to see if this comment's (now known as a reply's) id matches 
                // the id of the comment currently in the position in the list of comments
                if($row['parent_id'] == $potential_parent['discussion_id']) {
                    //
                    $reply_each = array();
                    //
                    $reply_each['reply_id'] = $row['course_discussion_id'];
                    $reply_each['reply_owner_f_name'] = $row['f_name'];
                    $reply_each['reply_owner_l_name'] = $row['l_name'];
                    $reply_each['reply_text'] = $row['text'];
                    $reply_each['reply_posted_datetime'] = $row['posted_datetime'];
                    $reply_each['reply_is_reply'] = $row['is_reply'];
                    $reply_each['reply_parent_id'] = $row['parent_id'];
                    //
                    array_push($potential_parent['discussion_replies_array'], $reply_each);
                }
            }
        }
    }
    //
    foreach($recent_discussions_array as $recent_discussion) {
        echo ($recent_discussion['discussion_id'].' has the following replies: ');
        foreach($recent_discussion['discussion_replies_array'] as $reply) {
            echo($reply['reply_id']);
        }
    }
    //
    return $recent_discussions_array;
    //
    // end
    //
}
  • 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-06-06T08:26:35+00:00Added an answer on June 6, 2026 at 8:26 am

    foreach creates a copy of items for usage inside the loop body. What this means is that $potential_parent is not actually the original arrays from $recent_discussions_array, but copies of them.

    Thus, when you modify them, as soon as the foreach($recent_discussions_array as $potential_parent) loop finishes, those copies go away.

    As a simplified example of what I mean, note the distinct lack of any “baz” printing here:

    http://ideone.com/WlDru

    Then compare to what happens if you actually access the original array by key:

    http://ideone.com/etvr5

    What you really probably want, as Ben points out in the comments, is to use the & operator:

    http://ideone.com/2TphI

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

Sidebar

Related Questions

So I have this weird looking problem: my very basic program generates an error
This is very weird. I have the following code: Assert.AreEqual(new DateTime(2000, 1, 1), DateTime.ParseExact(2000,
I have a very weird thing to do and simply don't know how. This
I have a very weird error with c++. I have two values, max and
ohk i am facing this very weird problem . i have a cloud server
This problem is very weird to me. I am using jQuery to submit some
I have a file that I am getting a very weird error on. The
So I have a very weird problem. I am writing some code in VB.Net
I have this very simple jQuery function: $(.milestone-in-tree).live({ mouseenter: function() { setTimeout( $.ajax({ type:
I have this very simple br2nl function that I use to take a string

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.