views:

63

answers:

3

How can I copy an array that has other associative arrays in it? I am talking about a result set returned from a mysql_fetch_assoc.

So say I have a structure like this...

connect
$result = query;

while ($row = mysql_fetch_assoc($result)) {

    array_push($static_row, $row); // here lies the problem

}

I would like to get that $static_row exactly as a copy of $row. Eventually I would like to put that query and while loop in a function, and simply return $static_row

As a reference, a print_r of $row looks like this

Array ( [key1] => value1 [key2] => value2 )
Array ( [key1] => value1 [key2] => value1 )

Thanks, let me know if you need more details

+1  A: 

Well, I'm not exactly sure what you're trying to do, but it looks like you have the copy assignment (which should work just like that) inside the loop. Maybe that is your problem.

Franz
oh my...excuse me...made a little boo boo therethis is what goes inside the while loop...array_push($static_row, $row);let me edit the thing above
@unknown, then edit your question.
Don
you know what...nvm...I was making a very stupid mistake that I fixed just now...it works like it should
+3  A: 

Use the form:

connect $result = query; 
while ($row = mysql_fetch_assoc($result))
{ 
   $rows[] = $row;
}
// now you have all the answers in an array of arrays, which you can return from a function
Don
+1 That's an even better way than `array_push`.
Franz
A: 

right from the php doc page

// Fetching all the results to array with one liner: 
$result = mysql_query(...); 
while(($resultArray[] = mysql_fetch_assoc($result)) || array_pop($resultArray))
Scott Evernden