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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.