www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Looping foreach as a group

  1. #1
    Join Date
    Aug 2004
    Posts
    47

    resolved [RESOLVED] Looping foreach as a group

    This feels like a stupid question, but it's one that's beyond me at the moment.

    I want to build table rows using 3 different arrays, one for each table cell.

    Code:
    foreach ($array_1 as $this_1)
    {
    print "<td>Blah1</td> \n";
    }
    
    foreach ($array_2 as $this_2)
    {
    print "<td style=\"text-align:center;\">Blah2</td> \n";
    }
    
    foreach ($array_3 as $this_3)
    {
    print "<td style=\"text-align:center;\">Blah3</td></tr> \n";
    }
    Of course, what's happening is it's doing 30 cells for $array_1, then 30 cells for $array_2 and then 30 cells for $array_3. I'd like it to do 1 cell from the first array, 1 cell from the second array, one cell from the third array, repeat 30 times.

    Help much appreciated, thank you.
    Last edited by TygerTyger; 12-17-2006 at 03:14 PM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,529
    PHP Code:
    echo "<table>\n";
    while(list(, 
    $this_1) = each($array_1) and
          list(, 
    $this_2) = each($array_2) and
          list(, 
    $this_3) = each($array_3))
    {
       echo 
    "<tr><td>$this_1</td><td>$this_2</td><td>$this_3</td><tr>\n";
    }
    echo 
    "</table>\n"
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Posts
    47
    Thank you very much indeed, just the ticket.

  4. #4
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    You must ensure that your three arrays are structurally identical at all times...

  5. #5
    Join Date
    Aug 2004
    Posts
    47
    Quote Originally Posted by NightShift58
    You must ensure that your three arrays are structurally identical at all times...
    Well, naturally.

    Since there's still replies going on, how do I assign an IF to be true if the loop is at $variable_15 (or whatever) in the array?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,529
    PHP Code:
    while(list($ix$this_1) = each($array_1) and
          list(, 
    $this_2) = each($array_2) and
          list(, 
    $this_3) = each($array_3))
    {
       if(
    $ix == 15)
       {
          
    // do something....
       
    }
       
    // rest of while loop....

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    That's part of the hidden dangers with the code posted earlier. It just assumes everything is in order, that there are no "gaps" in the arrays, etc...

    But... If your three arrays are REALLY, TRULY ALWAYS going to be synchronized, using a FOR loop strategy would be just as good and there would be no hidden traps.

    PHP Code:
    $maxCOUNT max(count($array_1),count($array_2),count($array_3));
    FOR (
    $loop 0$loop <= $maxCOUNT$loop++) :
      echo 
    "<tr>";
      echo 
    "<td>" . (isset($array_1[$loop]) ? $array_1[$loop] : "&nbsp;") . "</td>";
      echo 
    "<td>" . (isset($array_2[$loop]) ? $array_2[$loop] : "&nbsp;") . "</td>";
      echo 
    "<td>" . (isset($array_3[$loop]) ? $array_3[$loop] : "&nbsp;") . "</td>";
      echo 
    "</tr>";
      IF (
    $loop == 15) :
        
    // Perform action15
      
    ENDIF;
    ENDFOR; 
    In the previously posted example, if some reason $array_3[5] were not to be set, the entire WHILE would come to a screeching halt. If you prefer using that code, you should consider changing the AND condition to an OR. this would ensure that the loop woud continue to proceed as long as data kept coming in from a least one of the arrays.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles