cujo Posted April 30, 2006 Share Posted April 30, 2006 I have a foreach loop that enters the values of an 3-dimensional array into a table. It's something like this:[code]foreach($thearray[$val1][$val2] as $value){ mysql_query("INSERT INTO table VALUES ($value, etc, etc)");}[/code]When I look at the table, the first few values are Array[1][1], Array[1][2], etc. followed by the correct values. Does anyone know how to fix this? Link to comment https://forums.phpfreaks.com/topic/8736-bogus-array-values/ Share on other sites More sharing options...
KrisNz Posted April 30, 2006 Share Posted April 30, 2006 [!--quoteo(post=369990:date=Apr 30 2006, 10:24 AM:name=cujo)--][div class=\'quotetop\']QUOTE(cujo @ Apr 30 2006, 10:24 AM) [snapback]369990[/snapback][/div][div class=\'quotemain\'][!--quotec--]I have a foreach loop that enters the values of an 3-dimensional array into a table. It's something like this:[code]foreach($thearray[$val1][$val2] as $value){ mysql_query("INSERT INTO table VALUES ($value, etc, etc)");}[/code]When I look at the table, the first few values are Array[1][1], Array[1][2], etc. followed by the correct values. Does anyone know how to fix this?[/quote]Add another foreach inside your first one[code]foreach($thearray as $value){ foreach($value as $finalVal) { mysql_query("INSERT INTO table VALUES ($finalVal, etc, etc)"); }}[/code]Sorry, you shouldn't have [] in a foreach. Link to comment https://forums.phpfreaks.com/topic/8736-bogus-array-values/#findComment-32067 Share on other sites More sharing options...
cujo Posted April 30, 2006 Author Share Posted April 30, 2006 That didn't work. The values in the table are literally printed as Array[1][1], Array[1][2]. Link to comment https://forums.phpfreaks.com/topic/8736-bogus-array-values/#findComment-32068 Share on other sites More sharing options...
michaellunsford Posted April 30, 2006 Share Posted April 30, 2006 I'd nest a do while loop -- my code might be sloppy, but it should work.[code]do { do { mysql_query("INSERT INTO `table` VALUES (".$thearray[key($thearray)][$i].")"); } while(is_set($thearray[key($thearray)][++$i]));} while(next($thearray));[/code]you might have to hack at it a bit -- but you get the idea. Link to comment https://forums.phpfreaks.com/topic/8736-bogus-array-values/#findComment-32081 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.