As an example I put this foreach loop:
$colores = array(
"rojo",
"verde",
"azul",
"amarillo"
);
foreach ($colores as $valor) {
echo '<span class="clase">' . $valor . '</span>';
}
Al <span>
, instead of class="class" I want to apply a class to every second value:
class="izquierda"
class="derecha"
So that every second value is left-aligned and every remaining second is right-aligned.
You can check the position as you walk through the array, and see if it's even or odd.
Based on @Carmen's answer, this would do the same thing but with fewer lines of code:
Also, you can apply different styles to html elements using only css using the pseudo-class :nth-child and the odd (odd) and even (even) values.
For example: