Jump to content


Photo

Bogus array values


  • Please log in to reply
3 replies to this topic

#1 cujo

cujo
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 30 April 2006 - 12:24 AM

I have a foreach loop that enters the values of an 3-dimensional array into a table.

It's something like this:
foreach($thearray[$val1][$val2] as $value){
   mysql_query("INSERT INTO table VALUES ($value, etc, etc)");
}

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?

#2 KrisNz

KrisNz
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • LocationAuckland, New Zealand

Posted 30 April 2006 - 12:35 AM

[!--quoteo(post=369990:date=Apr 30 2006, 10:24 AM:name=cujo)--][div class=\'quotetop\']QUOTE(cujo @ Apr 30 2006, 10:24 AM) View Post[/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:
foreach($thearray[$val1][$val2] as $value){
   mysql_query("INSERT INTO table VALUES ($value, etc, etc)");
}

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
foreach($thearray as $value)
{
  foreach($value as $finalVal)
   {
     mysql_query("INSERT INTO table VALUES ($finalVal, etc, etc)");
   }
}
Sorry, you shouldn't have [] in a foreach.

#3 cujo

cujo
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 30 April 2006 - 12:38 AM

That didn't work. The values in the table are literally printed as Array[1][1], Array[1][2].

#4 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 30 April 2006 - 02:02 AM

I'd nest a do while loop -- my code might be sloppy, but it should work.

do {
      do {
         mysql_query("INSERT INTO `table` VALUES (".$thearray[key($thearray)][$i].")");
      } while(is_set($thearray[key($thearray)][++$i]));
} while(next($thearray));

you might have to hack at it a bit -- but you get the idea.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users