English 中文(简体)
如何将“传道——大号”混为一谈,以在另一场之后不得出相同结果?
原标题:How to configure php s "array_rand" to not give 2 same results on after another?

I use php array_rand to selected 1 arbitrary Records from range, 例:

$style_class = array("st_style1","st_style2","st_style3","st_style4");
$random_class = array_rand($style_class, 1);
$div_class = $style_class[$random_class];

问题在于,有时它有同样的记录,而且由于我只使用4个记录,它经常发生平静(使用“传闻”并非必然)。

例:

st_style1, st_style2, st_style2, st_style2, st_style4, st_style2 ...

如果能解决这个问题,那么两张记录不会连续两度出现。

例如

st2、中型4、中型2、中型2、中型1、中型3、中型2、中型1

最佳回答

最简单的解决办法是跟踪最新情况,在你有别之前保持随机呼吁。 类似:

$style_class = array("st_style1","st_style2","st_style3","st_style4");
$styles = array()
$lastStyle = -1
for($i = 0; $i < 5; $i++) {
    while(1==1) {
        $newStyle = array_rand($style_class, 1);
        if ($lastStyle != $newStyle) { 
            $lastStyle = $newStyle;
            break;
        }
    }
    $div_class = $style_class[$lastStyle]
    $styles[] = $div_class
}

Then use the $styles[] array in order. It should not have any duplicates

问题回答

除了最后的风格外,新风格与最后风格有所不同。 那么,就每处处决而言,你将有所不同。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签