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

  • Home
  • SEARCH
  • 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 8943581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:48:38+00:00 2026-06-15T11:48:38+00:00

In the following code, the first print_r inside the while loop prints different story

  • 0

In the following code, the first print_r inside the while loop prints different story content. The problem I am having is that the 2nd print_r statement produces the exact same story over and over from the $stories array.

$stories = array();

while($row = mysql_fetch_array($result)){
  $story->name      = $row['Name'];
  ...
  $story->date      = $row['Date'];

  print_r($story); //for testing
  array_push ( $stories , $story );
}

print_r($stories);

Edit:
someone asked for command line output, but this is a hosted account. In case the above is not clear though:

From inside loop:

(
    [id] => 9370
    [name] => Five Below, Inc.
    ...
)
stdClass Object
(    
    [id] => 9362
    [name] => Peregrine Pharmaceuticals Inc.
    ...
)   
stdClass Object
(
    [id] => 9363
    [name] => Mitel Networks Corporation
)
...
stdClass Object
(
    [id] => 9370
    [name] => Five Below, Inc.
    ...
)

After loop:

Array
(
    [0] => stdClass Object
        (
            [id] => 9370
            [name] => Five Below, Inc.
            ...
        )
    [1] => stdClass Object
        (
            [id] => 9370
            [name] => Five Below, Inc.
        ) 
    [2] => stdClass Object
        (
            [id] => 9370
            [name] => Five Below, Inc.
        )
  • 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-15T11:48:39+00:00Added an answer on June 15, 2026 at 11:48 am

    It seems the problem lies in $story. I made test case code:

    $rows = array(
             array('Name'=>'1', 'Date'=>'21'),
             array('Name'=>'4', 'Date'=>'24'),
    );
    
    $stories = array();
    
    foreach( $rows as $row ) {
      $story->name      = $row['Name'];
      $story->date      = $row['Date'];
    
      array_push( $stories , $story );
    }
    
    print_r($stories);
    

    produces

    [0] => stdClass Object
        (
            [name] => 4
            [date] => 24
        )
    [1] => stdClass Object
        (
            [name] => 4
            [date] => 24
        )
    

    which is wrong. However adding unset($story); and have new object create each time, solves the issue:

    $rows = array(
             array('Name'=>'1', 'Date'=>'21'),
             array('Name'=>'4', 'Date'=>'24'),
    );
    
    $stories = array();
    
    foreach( $rows as $row ) {
      unset( $story );   // replace with whatever code you use to create new object
      $story->name      = $row['Name'];
      $story->date      = $row['Date'];
    
      array_push( $stories , $story );
    }
    
    print_r($stories);
    

    gives correct:

    [0] => stdClass Object
        (
            [name] => 1
            [date] => 21
        )
    
    [1] => stdClass Object
        (
            [name] => 4
            [date] => 24
        )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code (without showing it all) is my first attempt at using classes.
How to DRY the following code and call the first initializer from the second?
Does the following code contain a memory leak of the first three characters in
I'm using the Code First approach and have the following Model: public class Person
In the following code, the first log statement shows a decimal as expected, but
I have the following code: Public Shared Function GetAvailableManufacturers() As List(Of Manufacturer) 'first get
I've in the following code the progressbar working on the first page load. after
The following is using EF 5.0.0-rc and Code First. In my design, I have
I've written the following code to create a three-column layout where the first and
In the following code snippet, if I go to the first open parenthesis (

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.