473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

nested foreach

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
1 6738
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tony | last post by:
I'm trying to simplify a list of statements similar to the following: document.getElementById('header').style=default.header; document.getElementById('header').style=default.header;...
6
by: Andrew Matthews | last post by:
Hi All, I have the following little class of iterators that allow me to iterate over elements in the file system. I have nested some of them, and then added Func<FileInfo, booldelegates to filter...
5
by: farrell.niall | last post by:
Hi, I'm trying to use nested foreach loops to loop through two arrays. Each element in the first array corresponds to another array. The brandcode part of the first array is the same as the name...
1
by: levidicom | last post by:
foreach($test as $var1){ foreach($test2 as $var2) { echo '"var1: " . $var1 . "<br>"var2: " . $var2 . "<br> \n"'; }
0
by: Sam | last post by:
Have been trying to do this in flexy for a while, but it does not seem to support it. I have an array of categories of which each contains an array of things. So I'd like the template to look like...
5
by: Ivan S | last post by:
I'm using next snippet: $somearray = array(...); foreach($somearray as $item1) { foreach($item1 as $item2) { // ... do something ... } }
15
by: fjm | last post by:
Hello everyone and happy Sunday. :) I have a problem that I *think* I may know the solution to but have no idea how to write the code for it. I am working on a templating system wher I have...
2
by: gomako | last post by:
Hi, I'm new, so please let me know if any of my forum etiquette is wrong! Apologies for the fairly nondescript subject line, but I am being driven insane by it. Anyhow, I have a form with...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.