[英]Probability spacing between 3 numbers and letters [closed]
我想像上面的例子一样在数字和字母之间建立概率间隔:
请注意,我有两个字符串,每个字符串包含3个字母:
$string1 = "123";
$string2 = "456";
//Result must be:
Line1: 1 2 3
Line2: 4 5 6
--------
$string1 = "456";
$string2 = "891";
//Result must be:
Line1: 46 5
Line2: 8 91
.....
如何使用PHP以编程方式执行此操作并返回这样的结果(使用rand函数)?
先感谢您。
随机化第一个,获取第一行的值的位置作为种子,并将其用于第二个字符串作为空格。 示例:(我只是用子弹代替空格)
// handle first line
$string1 = "123";
// create an array with the characters with spaces
$string1 = array_merge(str_split($string1), array_fill(0, 3, '•'));
shuffle($string1); // shuffle them first
// determine and sace the keys of the non empty characters
$empty_keys = array_keys($string1, '•');
$values = array_diff(array_keys($string1), $empty_keys);
$line1 = implode('', $string1);
// second line
$string2 = "456";
$temp = str_split($string2); // create a temporary holder
for($i = 0; $i < 6; $i++) {
if(isset($values[$i])) {
$line2[$i] = '•';
} else {
$line2[$i] = array_pop($temp);
}
}
$line2 = implode('', $line2);
echo $line1 . '<br/>' . $line2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.