shamuraq Posted June 4, 2009 Share Posted June 4, 2009 Objective: I just need to display random color value from a list of color in an array Scenario: <? $lebar = 10; for ($i = 0; $i <$lebar; $i++){ $color = array('#FFFFFF','#333333'); $color = shuffle($color); echo "$color<br>"; } ?> Output is: 1 1 1 1 1 1 1 1 1 1 Question: I simply want to display #FFFFFF or #333333... What went wrong in the code? Thanx in advance... Link to comment https://forums.phpfreaks.com/topic/160900-solved-php-shuffle/ Share on other sites More sharing options...
gevans Posted June 4, 2009 Share Posted June 4, 2009 were you looking for something like this? <?php $color = array('#FFFFFF','#333333'); $lebar = count($color); shuffle($color); for ($i = 0; $i <$lebar; $i++){ echo "{$color[$i]}<br>"; } If you just wanted to alternate use the following; <?php $color = array('#FFFFFF','#333333'); $lebar = 10; for ($i = 0; $i <$lebar; $i++){ if(!($i%2)) echo "{$color[0]}<br>"; else echo "{$color[1]}<br>"; } Link to comment https://forums.phpfreaks.com/topic/160900-solved-php-shuffle/#findComment-849129 Share on other sites More sharing options...
kickstart Posted June 4, 2009 Share Posted June 4, 2009 Hi Or for 10 random ones <? $lebar = 10; for ($i = 0; $i <$lebar; $i++){ $color = array('#FFFFFF','#333333'); $color = shuffle($color); echo $color[0]."<br>"; } ?> All the best Keith Link to comment https://forums.phpfreaks.com/topic/160900-solved-php-shuffle/#findComment-849130 Share on other sites More sharing options...
gevans Posted June 4, 2009 Share Posted June 4, 2009 Keith, that wont work, suffle() returns an integer and will change the array '$color' to a variable holding either a 1 or a 0 (1 for success of the shuffle()) Link to comment https://forums.phpfreaks.com/topic/160900-solved-php-shuffle/#findComment-849134 Share on other sites More sharing options...
kickstart Posted June 4, 2009 Share Posted June 4, 2009 Hi You right. Brain fade. Need to take the assingment back to $color out. <? $lebar = 10; for ($i = 0; $i <$lebar; $i++){ $color = array('#FFFFFF','#333333'); shuffle($color); echo $color[0]."<br>"; } ?> All the best Keith Link to comment https://forums.phpfreaks.com/topic/160900-solved-php-shuffle/#findComment-849141 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.