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... Quote Link to comment 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>"; } Quote Link to comment 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 Quote Link to comment 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()) Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.