Jump to content

Archived

This topic is now archived and is closed to further replies.

cujo

Bogus array values

Recommended Posts

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?

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites
That didn't work. The values in the table are literally printed as Array[1][1], Array[1][2].

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.