...

View Full Version : About foreach loop



kesikkafa
01-03-2006, 08:29 PM
can i use 2 arrays in one foreach loop? i mean something like this ;


foreach($grades as $grade and $courses as $course)

{
...
}

Prikid
01-03-2006, 08:44 PM
As far as I know, no.

gsnedders
01-03-2006, 09:00 PM
while (list(, $grade) = each($grades) && list(, $course) = each($courses))

{
...
}
Would do what you're trying to do.

ralph l mayo
01-04-2006, 12:44 AM
Also if the arrays directly correspond, which I assume they do, since it doesn't make sense to process them concurrently if not, you can do something like



foreach ($grades as $key=>$grade)
{
// $class' corresponding element to the current $grade is $class[$key]
}


or:



$keys = count($grades);
for ($ii = 0; $ii < $keys; ++$ii)
{
// corresponding elements are $grades[$ii] and $class[$ii]
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum