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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:10:18+00:00 2026-05-26T03:10:18+00:00

I really dont understand the output of my array. In the start it seams

  • 0

I really dont understand the output of my array. In the start it seams to be very simple but I am already some hours in this problem. Look the code:

public function getAllProducts($limit){
$result = mysql_query("SELECT * FROM products ORDER BY RAND() LIMIT $limit") or die(mysql_error());
        $productArray = array();
        $count = 0;
        while($row = mysql_fetch_row($result)){
            $this->id = $row[0];
            $this->category_id = $row[1];
            $this->title = $row[2];
            $this->short_description = $row[3];
            $this->long_description = $row[4];
            $this->tag = $row[5];
            $this->price = $row[6];
            $this->weight = $row[7];
            $this->stock = $row[8];   
            $productArray[$count] = $this;
            $count++;
        }

            echo'<pre>';
            print_r($productArray);
            echo'</pre>';

        return $productArray;

    }

The output:

Array
(
    [0] => Product Object
        (
            [id] => 2
            [category_id] => 2
            [title] => Cart�o de Pascoa
            [short_description] => Short description bout this product 
            [long_description] => Long description about thi product but we don
            [tag] => Pascoa Coelho Cart�o Cartao Card
            [price] => 60,00
            [weight] => 1
            [stock] => 1
        )

    [1] => Product Object
        (
            [id] => 2
            [category_id] => 2
            [title] => Cart�o de Pascoa
            [short_description] => Short description bout this product 
            [long_description] => Long description about thi product but we don
            [tag] => Pascoa Coelho Cart�o Cartao Card
            [price] => 60,00
            [weight] => 1
            [stock] => 1
        )

)

Now, I will do only an ajustment in the print_r function:

 echo'<pre>';
 print_r($productArray[0]);
 echo'</pre>';

The New Output:

Product Object
(
    [id] => 1
    [category_id] => 1
    [title] => Cart�o de Natal
    [short_description] => Short descroption about this product
    [long_description] => Long description of this product. Nor used ri
    [tag] => Cart�o de Natal Natal Presente de Natal
    [price] => 55,00
    [weight] => 1
    [stock] => 1
)

One more adjust:

echo'<pre>';
print_r($productArray[1]);
echo'</pre>';

The OutPut:

Product Object
(
    [id] => 2
    [category_id] => 2
    [title] => Cart�o de Pascoa
    [short_description] => Short description bout this product 
    [long_description] => Long description about thi product but we don
    [tag] => Pascoa Coelho Cart�o Cartao Card
    [price] => 60,00
    [weight] => 1
    [stock] => 1
)

The DataBase:

1   1   Cartão de Natal Short descroption about this product    Long description of this product. Nor used ri   Cartão de Natal Natal Presente de Natal 55,00   1   1

2   2   Cartão de Pascoa    Short description bout this product Long description about thi product but we don   Pascoa Coelho Cartão Cartao Card    60,00   1   1

Have you noticed that when I do a print_r or a var_dump in the $productArray we have an wrong output and when we do $productArray[0] or $productArray[1] we get the correct one. Have anyone noticed something wrong in my code?

Thanks a lot in advanced!

  • 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-26T03:10:19+00:00Added an answer on May 26, 2026 at 3:10 am

    In PHP5, objects are not copied when they are inserted into arrays or passed to functions. So you are only inserting one object multiple times into the array, and then overwriting its properties for each loop iteration.

    You should either:

    1) Create a new instance of the class:

        while($row = mysql_fetch_row($result)){
            $object = new self();
            $object->id = $row[0];
            $object->category_id = $row[1];
    

    2) Or clone the object when you insert it into the array:

            $this->stock = $row[8];   
            $productArray[$count] = clone $this;
            $count++;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I dont really understand why this is not working, I am being returned to
I really don't understand this. I have a simple ASP with 3 div in
Sorry for unclear subject but i really don't understand where problem. I use a
I think shoulda is really neat, but what I don't understand is why some
I'm really sorry to have to ask this, but I clearly don't understand something
I really dont understand why this is happening, bash seems to remove commands while
Really quick question here, some other people helped me on another problem but I
I dont really understand whats going on and cant see the difference: I'm downloaded
I am currently learning bash programming and dont really understand why the passing argument
Possible Duplicate: What is recursion really and explain the output of this program? What

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.