|
Posted by niallfarrell on 03/22/07 21:38
On Mar 22, 9:23 pm, Shimin <smguo2...@gmail.com> wrote:
> 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($curbrand as $key => $value)
>
> > Hopefully this is what you are looking for.
>
> > On Mar 22, 12:17 pm, farrell.ni...@gmail.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.
Navigation:
[Reply to this message]
|