Jump to content

Bogus array values


cujo

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.