immanuelx2 Posted March 23, 2009 Share Posted March 23, 2009 I understand the easy way to fetch an array is to use a while loop ex: while ($row = mysql_fetch_array($query)) However, if I want to use that same array more than once, I should dump it into a separate array like so (correct?): $tables = array(); $query = mysql_query("...") or die(mysql_error()); while ($row = mysql_fetch_array($query)) $tables[] = $row[0]; That works, however, later down the line when I use the foreach ($tables as $key => $value) loop to populate a select form element, it only uses it's numerical index instead of the index assigned by the query. My question is, how do I maintain the index from the query? (for example $row['id'] or $row['name'] instead of $row[0]) Thanks in advance Link to comment https://forums.phpfreaks.com/topic/150650-solved-mysql_fetch_array-into-an-array-to-use-more-than-once/ Share on other sites More sharing options...
trq Posted March 23, 2009 Share Posted March 23, 2009 while ($row = mysql_fetch_assoc($query)) $tables[] = $row; } foreach ($tables as $table) { echo $table['name'] . $table['id']; } Link to comment https://forums.phpfreaks.com/topic/150650-solved-mysql_fetch_array-into-an-array-to-use-more-than-once/#findComment-791437 Share on other sites More sharing options...
immanuelx2 Posted March 23, 2009 Author Share Posted March 23, 2009 you are a gentleman and a scholar. Link to comment https://forums.phpfreaks.com/topic/150650-solved-mysql_fetch_array-into-an-array-to-use-more-than-once/#findComment-791489 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.