|
|
| Home |
| previous| next |
Loop through Array with Alternating Colors |
|
This PHP snippet shows 2 methods for displaying alternative row colors while looping through an array. In the examples listed here only the font colors are changed. In reality you can change anything you like based on the outcome of the calculations. <?php // method 1 $a = array ('one','two','three','four'); foreach ($a as $b => $c) { $color = ( is_int($b / 2) ) ? 'red' : 'blue'; echo '<span style="color: '. $color .'">'. $c .'</span><br />'."n"; } // method 2 - no need to know: use counter $i = 0; foreach ($a as $b => $c) { $color = ( ++$i & 1 ) ? 'red' : 'blue'; echo '<span style="color: '. $color .'">'. $c .'</span><br />'."n"; } |
| Tag: PHP |
| This entry was posted on Saturday, March 1st, 2008 at 3:57 pm and is filed under Dev |
0 comments |
| previous| next |
| Mobile | Standard |