473,799 Members | 3,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6755
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
25393
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; document.getElementById('header').style=default.header; document.getElementById('middle').style=default.middle; document.getElementById('middle').style=default.middle; document.getElementById('middle').style=default.middle;...
6
1570
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 out unwanted files. I get an InvalidProgramException and haven't (yet) been able to find out what's going on. Do you have any ideas? TIA Andrew Matthews
5
4085
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 of the corresponding array. Basically, one array is of the brand info, and the second is of the cars made by that brand. To loop through all the brands, and then print their corresponding cars, I tried: foreach ($brands as $curbrand) {
1
1805
by: levidicom | last post by:
foreach($test as $var1){ foreach($test2 as $var2) { echo '"var1: " . $var1 . "<br>"var2: " . $var2 . "<br> \n"'; }
0
3778
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 this. {foreach:categories,category} {foreach:category,thing} <htmltags>{thing->someField}</htmltags>... {end:} {end:} Now it seems that flexy does not translate any flexy "statements" inside a foreach-statement. So it does not work in this way.
5
5247
by: Ivan S | last post by:
I'm using next snippet: $somearray = array(...); foreach($somearray as $item1) { foreach($item1 as $item2) { // ... do something ... } }
15
2756
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 created a template html file. I am using str_replace to replace the fields that I want changed. I'm not sure that I am doing this correctly either. A code sample follows. I have a script that pulls a single customer from the database. The data that is...
2
4465
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 multiple checkboxes that need to be sticky, so I check the checkbox $_POST array against the array of form elements using a nested foreach loop. The trouble is, that when the form is submitted, even though all the checked boxes are in the $_POST array,...
0
9688
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10268
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10247
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9079
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.