NYCPHP Meetup

NYPHP.org

[nycphp-talk] for() question

Scott Mattocks crisscott at netzero.com
Tue Apr 20 14:55:52 EDT 2004


Rafi Sheikh wrote:

> Now, It is very embarrassing but after I get the data read in (code below),
> I for the love of PHP, can not make unique rows or dynamically assign the
> data.  I tried the following:
> $plot=array();
> $i=0;
> 
>  while ($row = mysql_fetch_assoc($resultID)) {
> 
> 	foreach($row as $field) {
> 	$plot[$i]=$field;
> 	$i++;
> 	}

The problem is that $field is a reference. You are assigning the value 
of $plot[$i] to the refernce $field.  The place that $field points 
changes with every iteration of the foreach loop.  That is why all of 
your entries in plot end up being the same.  Try this:

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

  	foreach($row as $key => $field) {
  	$plot[$i]=$row[$key];
  	$i++;
  	}

It isn't the cleanest or most elegant but I think it should work for 
what you need.

Scott Mattocks



More information about the talk mailing list