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) {
foreach (${$curbrand['brandcode']} as $curcar) {
echo $curcar;
}
}
But I get "Invalid argument supplied for foreach()"
Any ideas? 5 4086
This should work for you...
foreach ($brands as $curbrand) {
foreach ($curbrand as $brandcode =$curcar) {
echo $curcar;
}
}
the syntax above in the inner loop is for when you have key value
pairs in the array of interest.
so that line could be rewritten, foreach($curbra nd as $key =$value)
Hopefully this is what you are looking for.
On Mar 22, 12:17 pm, farrell.ni...@g mail.com wrote:
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) {
foreach (${$curbrand['brandcode']} as $curcar) {
echo $curcar;
}}
But I get "Invalid argument supplied for foreach()"
Any ideas?
Clay Colburn wrote:
This should work for you...
foreach ($brands as $curbrand) {
foreach ($curbrand as $brandcode =$curcar) {
echo $curcar;
}
}
the syntax above in the inner loop is for when you have key value
pairs in the array of interest.
so that line could be rewritten, foreach($curbra nd as $key =$value)
Hopefully this is what you are looking for.
On Mar 22, 12:17 pm, farrell.ni...@g mail.com wrote:
>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 correspondin g 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) { foreach (${$curbrand['brandcode']} as $curcar) { echo $curcar; }}
But I get "Invalid argument supplied for foreach()" Any ideas?
It's clearly not what the OP is looking for.
On Mar 22, 9:23 pm, Shimin <smguo2...@gmai l.comwrote:
Clay Colburn wrote:
This should work for you...
foreach ($brands as $curbrand) {
foreach ($curbrand as $brandcode =$curcar) {
echo $curcar;
}
}
the syntax above in the inner loop is for when you have key value
pairs in the array of interest.
so that line could be rewritten, foreach($curbra nd as $key =$value)
Hopefully this is what you are looking for.
On Mar 22, 12:17 pm, farrell.ni...@g mail.com wrote:
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) {
foreach (${$curbrand['brandcode']} as $curcar) {
echo $curcar;
}}
But I get "Invalid argument supplied for foreach()"
Any ideas?
It's clearly not what the OP is looking for.
No, that didn't do it but thanks anyway. I figured it out eventually -
this seems to do the trick:
foreach ($brands as $curbrand) {
$cars = ${$curbrand[brandcode]};
echo $curbrand[brandname];
foreach ($cars as $curcar) {
echo $curcar;
}
}
It basically echoes the brandname followed by all the cars in the
array of the same name. $cars = ${$curbrand[brandcode]} was the main
bit I was looking for - assigning a variable as the array name.
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) {
foreach (${$curbrand['brandcode']} as $curcar) {
echo $curcar;
}
}
But I get "Invalid argument supplied for foreach()"
Any ideas?
I am not 100% sure I have understood your problem but the following works
for the way I envisaged your arrays:-
$brands = array('porsche' , 'ford');
$porsche = array('911');
$ford = array('escort') ;
foreach ($brands as $r)
{
foreach (${$r} as $model)
{
echo $model;
}
}
The error you have suggests that you are not passing an array to the foreach
loop instead you are passing a value.
On Thu, 22 Mar 2007 22:23:56 +0100, Shimin <sm*******@gmai l.comwrote:
It's clearly not what the OP is looking for.
I love it when people state this, and fail to provide an explanation, let
alone state what, according to them, is a correct answer to what the OP is
looking for.
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: N. David |
last post by:
In bash, you can include a variable as part of a filename. For
example:
date=$(date)
cat log.txt > $date"_log.txt"
Which, when setting the right options in date, creates a file named
20040705_log.txt
I am now trying to call a class based on a previously defined variable
|
by: Nico |
last post by:
Hello folks,
I am currently storing a set of objects inside an array,
$itemlist = array();
$itemlist = new item("myitem");
//...
and I am looking to develop a search function, which returns a
reference to the found item.
|
by: Dave Smithz |
last post by:
Hi there,
I have a php script that does some form input validation. As it verifies
each field if the field has incorrect data, it appends an error message to
an $error array. E.g.
if (an_error = true) {
$errors.="<tr><td>You have note entered a surname. This is mandatory date
of birth incorrectly.</td></tr>";
}
|
by: manish |
last post by:
I have created a function, it gives more readability compared to the
print_r function. As of print_r, it works both for array or single
variable. I just want to add in it, the opton to view the variable
name for the case of non array variables. Also I want to show the
array name.
Is there any way that the variable name that is passed to the function
can be displayed.
function formattedoutput($object) {
|
by: cody |
last post by:
why foreach does always have to declare a new variable?
I have to write
foreach (int n in array){}
but Iam not allowed to write:
int n=0;
foreach (n in array){}
| |
by: walterbyrd |
last post by:
I know I can use the file() function to read a file into an array by
providing a literal file name between single quotes:
$lines = file('literal_file_name');
But, what if I want file() to use a variable name?
$variable_file_name = "literal_file_name";
$lines = file($variable_file_name);
|
by: sicapitan |
last post by:
I'm trying to build some xml from an array with the help from another
array.
In one array I have
$cells
Array
(
=CELL0
|
by: strawberry |
last post by:
In the function below, I'd like to extend the scope of the $table
variable such that, once assigned it would become available to other
parts of the function. I thought 'global $table;' would solve this but
it's clear that I'm misunderstanding $variable persistence. I posted a
similar enquiry over at alt.php.mysql, but I guess this is a more
appropriate forum because the problems I'm having relate to PHP.
Any help appreciated.
...
|
by: Orin |
last post by:
Hi,
Here is my task:
- need to get text from a template variable.
For example we have a text with templates "Cars" and "Producers":
"{{Cars|Toyota=blablabla |Moskvitch=blablabla |BMW=blabla ...}}
{{Producers|TableLTD=blablabla |PhoneINC=Developed in 1998 in Kiev,
|
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...
|
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,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |