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

nested loop

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
le*******@gmail.com wrote:
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
What this is saying is that $test2 is not an array.

Also, $var1[1] will not work unless $test is a two dimensional array.
The same is true for $var2[1] and $test2.

There isn't anything special about nesting foreach() loops. Something like:

foreach ($array1 as $element1) {
foreach $array2 as $element2)

works just fine. It's just important to remember that array2 will be
processed in its entirety for each element of array1.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.