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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:24:30+00:00 2026-06-03T03:24:30+00:00

I want to push many objects into a array and each object have different

  • 0

I want to push many objects into a array

and each object have different value

but when I pushed them into array

all values of them are same

how to solve this problem?

$sql="select password, mail from account";
$result=mysql_query($sql);
$arr=array();
while($row=mysql_fetch_assoc($result))
{
    $o->pw=$row['password'];
    $o->mail=$row['mail'];
    array_push($arr, $o);
}
echo json_encode($arr);
  • 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-03T03:24:32+00:00Added an answer on June 3, 2026 at 3:24 am

    That’s because you are pushing the same object into the array each time.

    You should push a new object in each iteration instead. For example, if $o is a stdClass object, use $o = new stdClass inside the loop:

    while($row=mysql_fetch_assoc($result))
    {
        $o = new stdClass;
        $o->pw=$row['password'];
        $o->mail=$row['mail'];
        array_push($arr, $o);
    }
    

    You can also use mysql_fetch_object, which is perhaps more appropriate:

    while($o=mysql_fetch_object($result))
    {
        array_push($arr, $o);
    }
    

    The properties on the above object will be named based on your SQL query columns, so to achieve the same effect you would also need to change the query to select password AS pw, mail from account.

    Finally, another option is to clone the object each time — although the other alternatives are almost always preferable:

    while($row=mysql_fetch_assoc($result))
    {
        $o = clone $o;
        $o->pw=$row['password'];
        $o->mail=$row['mail'];
        array_push($arr, $o);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two or more javascript objects. I want to merge them adding values
I have a project that I want to push to a Mercurial repository on
Right now, I have a list of streams, and I want to push streams
I have a aplication and I want to have as many FLV players(to play
I have edited about 100 html files locally, and now I want to push
Generally whenever i want to push in the changes to remote Git i follow
Read about Server push here . I want to push data to client from
Howday, I want to push data to flex clients. I am talking about anywhere
I created a branch on my local Mercurial repository. I want to push to
I want to use push notification in my app.i want to know that do

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.