Re: Equivelant to mysql_fetch_row for normal array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dave M G wrote:
PHP List,

Very frequently I use mysql_fetch_row in a while statement to

while($variable = mysql_fetch_row($mysqlResult)){
echo $variable[text1];
echo $variable[text2];
}

Pretty standard.

But what do I do when I want to do the same kind of while loop not with a MySQL result, but with just a regular array?

while (variable = ????($array)){
echo $variable[text1];
echo $variable[text2];
}

I've been up and down the manual, and looked at foreach statements and functions like each(), but I can't seem to zero in on what I need.

Can anyone let me know what it is I'm missing?

Thank you for any advice.

--
Dave M G

If we "translate" a mysql resultset to an array notation, you would have:
restultset[1][text1] = a;
restultset[1][text2] = b;
restultset[2][text1] = c;
restultset[2][text2] = d;
restultset[3][text1] = e;
restultset[3][text2] = f;
Thus, going trough it with a foreacht:
foreach(resultset as $key=>$val) {
   echo $val[text1];
}
will give you the same result as the original loop construct you did with while() (assuming an array resultset for purposes of demonstration).

foreach($mysqlResultSet as $variable) {
   echo $variable[text1];
   echo $variable[text2];
}

is what you're probably after though. Might I suggest reading the manual about foreach (and especially array) construct again please? very carefuly preferably, as it seems you're missing quite a lot of insight there :)

- tul

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux