By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,962 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,962 IT Pros & Developers. It's quick & easy.

nested foreach

P: n/a
foreach($test as $var1){

foreach($test2 as $var2)
{

echo '"var1: " . $var1[1] . "<br>"var2: " . $var2[1] . "<br>
\n"';
}
}

--------------------

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

May 20 '07 #1
Share this Question
Share on Google+
1 Reply


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

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
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.

HTH
Sh.

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.
May 20 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.