使用功能,但如果它与之前的功能具有相同的结果,则再次运行

时间:2013-09-03 19:34:40

标签: php

所以我已经使用了随机颜色的功能。问题是我将打印相同的功能8次,我不希望两个打印返回相同的变量。所以基本上我希望它再次运行该功能,如果它之前已打印过颜色。

function random_color(){
$color_numb = "8";

$color = rand(1, $color_numb);

if($color == "1"){
  $type = "Blue";
}
if($color == "2"){
  $type = "Red";
}
if($color == "3"){
  $type = "Orange";
}
if($color == "4"){
  $type = "Green";
}
if($color == "5"){
  $type = "Yellow";
}
if($color == "6"){
  $type = "Black";
}
if($color == "7"){
  $type = "Purple";
}
if($color == "8"){
  $type = "White";
}
return $type;

}
print random_color();
print random_color();
print random_color();
print random_color();
print random_color();
print random_color();
print random_color();
print random_color();

所以我知道如何才能做到这一点?

4 个答案:

答案 0 :(得分:3)

如果您需要不同颜色的8种不同颜色,只需创建数组,用颜色填充它,然后使用这种颜色填充(shuffle)数组 - http://php.net/manual/en/function.shuffle.php

$colors = array();
$colors[] = 'red';
$colors[] = 'green';
....
shuffle($colors);

答案 1 :(得分:0)

您也可以根据会话尝试:

<?php
//put this line at top of the page before printing anything
session_start();

function random_color(){
  $color_numb = "8";

  $random = rand(1, $color_numb);
  $colors  = array(1 => "Blue", "Red", "Orange", "Green", "Yellow", "Black", "Purple", "White");

  $current_color = $colors[$random];

  if(!isset($_SESSION['oldcolor'])){
    $_SESSION['oldcolor'] = $current_color;
    return $current_color;
  }
  while($current_color == $_SESSION['oldcolor']){
   $current_color = $colors[rand(1, $color_numb)];
  }
  $_SESSION['oldcolor'] = $current_color;

  return $current_color;
}

echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
echo random_color();
?>

答案 2 :(得分:0)

请尝试以下操作:

,而不是random_color()功能和8 print
$colors = array();
$colors[] = "Blue";
$colors[] = "Red";
$colors[] = "Orange";
$colors[]= "Green";
$colors[]= "Yellow";
$colors[] = "Black";
$colors[]= "Purple";
$colors[]= "White";
shuffle($colors);
for ($i = 0; $i < 8; $i++) {
   echo $colors[$i]
}

答案 3 :(得分:0)

如果您希望按需显示颜色,但从不显示重复的颜色,则可以执行以下操作:

Working jsFiddle here

<强> HTML:

<input id="mybutt" type="button" value="Show Next Color" />
<input id="listall" type="button" value="List All Found Values" />

<强>的Javascript / jQuery的:

var aCN = ['blue', 'red', 'orange', 'green', 'yellow', 'black', 'purple', 'white'];
var aFound = new Array();

$('#mybutt').click(function () {
    xx = 1;
    while (xx == 1) {
        getcol = random_color();
        if (aFound.indexOf(getcol) == -1) {
            alert(getcol);
            aFound.push(getcol);
            xx = 0;
        } else {
            if (aFound.length > 7) {
                alert('You already saw all values');
                xx = 0;
            }
        }
    }
});

$('#listall').click(function () {
    if (aFound.length == 0) {
        alert('Nothing seen yet...');
        return false;
    }
    Object.keys(aFound).forEach(function (key) {
        alert(aFound[key]);
    });
});

function random_color() {
    $color_numb = "8";
    $color = Math.floor(Math.random() * $color_numb);

    myCol = aCN[$color];
    return myCol + ' - ' + $color;
}