Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    About foreach loop

    can i use 2 arrays in one foreach loop? i mean something like this ;

    Code:
    foreach($grades as $grade and $courses as $course) 
    
    {
    ...
    }

  • #2
    New Coder
    Join Date
    Dec 2005
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As far as I know, no.

  • #3
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    PHP Code:
    while (list(, $grade) = each($grades) && list(, $course) = each($courses))

    {
    ...

    Would do what you're trying to do.

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    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

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

    or:

    PHP Code:
    $keys count($grades);
    for (
    $ii 0$ii $keys; ++$ii)
    {
        
    // corresponding elements are $grades[$ii] and $class[$ii]



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •