Jump to content

[SOLVED] PHP Shuffle


shamuraq

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.