Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
share|improve this question
"all values of them are same" what does that mean? Can you show a print_r() of the array? – Jakub May 1 '12 at 19:00
What's $o? Where's that coming from? – Rocket Hazmat May 1 '12 at 19:02
2  
I'm not sure but you probably have to create a new instance of the object. $o refers to the same object all the time. – Tibor May 1 '12 at 19:02
2  
If you're not using the return value from the array_push() function, it's faster to use the $arr[] = $o notation. – Crontab May 1 '12 at 19:07

5 Answers

Try to declare $o first (inside the while loop):

$o = new stdClass;
share|improve this answer

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);
}
share|improve this answer

This is because the object is being added to the array as a reference. Each element in the array is a reference to an object, the same object.

You didn't declare $o, so when you first do $o->pw, PHP creates an object for you. When it does this, it creates it outside the scope of the loop, so each iteration of the loop refers to the same $o.

You need to declare $o each loop iteration.

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

You don't really need to use push much in php, you can use the empty brackets to tack it on. Not sure if it makes a difference but I find the brackets easier. Also, O doesn't seem to be defined in this code, or reset in the loop. That is probably where the problem comes from, although i'm not very clear about your question overall. Good Luck

$sql="select password, mail from account";
$result=mysql_query($sql);
$arr=array();
while($row=mysql_fetch_assoc($result))
{
    //define/reset o here
    $o->pw=$row['password'];
    $o->mail=$row['mail'];
    $arr[] = $o;
}
echo json_encode($arr);
share|improve this answer

I think you need to instance a new object for each iteration of your loop. Right now there's only one $o being written to for each iteration of the loop, which is why they all appear to have the same value: they ARE the same.

Try this:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.