Reply to Re: Variable Array Name in foreach

Your name:

Reply:


Posted by peter on 03/22/07 21:50

> 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.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация