nested foreach

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
foreach($test as $var1){

    foreach($test2 as $var2)

                  echo '"var1: " . $var1[1] . "<br>"var2: " . $var2[1] . "<br>


The $tests have values  just that this nested loop doesnt work i keep
getting this error message

Warning: Invalid argument supplied for foreach()

What is the correct way to nest a foreach
Thanks alot

Re: nested foreach

At Sun, 20 May 2007 08:15:20 -0700, levidicom let his monkeys type:

Quoted text here. Click to load it

foreach($test as $var1){
   foreach($test2 as $var2)
      echo "var1: " . $var1 . "<br /> var2: " . $var2 . "<br>\n";
//or: echo "var1: $var1 <br /> var2: $var2 <br />";

First of all your string contains errors, leave out the single quotes, and
there was a " too many in "<br>" var2: ".  

Second, the foreach loops assign each array value to a variable ($var1 and
$var 2 in your case). Unless the original arrays contain subarrays in
each element, use $var1 and $var2 instead of $var1[index] and $var2[index].

Third, the error might indicate what you thought to be an array ($test,
$test2) might actually be another variable type.
using var_dump($test) or print_r($test) you can see what the $test
variable contains.


PS Posts sent to usenet (which is what you are doing right now, even when
you use google groups to send stuff to comp.lang.php) sometimes take some
time to show up. Posting twice doesn't change that ;-)

PPS. In case you plan to reply (please do), it's also considered best
practice/decent behaviour to reply BELOW the message. Google groups by
default let's you type a reply ABOVE it.

Site Timeline