jeudi 30 juin 2016

Select random string from array unless it equals something

I pick a random color in a foreach loop, how do i make sure that the chosen color isnt the last color that was chosen?

This is an example of my current code:

$array = array('a', 'b', 'c', 'd', 'e');
$colors = array('#61AE24', '#EAE672', '#00A1CB', '#E54028', '#E59B28', '#28E4E5', '#DB28E5');
foreach($array as $val) {
    $color = $colors[array_rand($colors)];
    echo '<div style="color: '.$color.';">'.$val.'</div>';
}

I need to make sure that the same color isnt used next to eachother.

Thanks




Aucun commentaire:

Enregistrer un commentaire