我想为该数组生成代码的所有组合
"2", "3", "4", "6", "7", "8", "9", "Q", "W", "R", "T", "Y", "P", "D", "F", "G", "H", "J", "K", "X", "C", "V", "B", "M"
有效代码必须具有25个simbol,并具有5个以“-”分隔的段
例如
34267-89QWR-TYPDF-GHJKX-CVBMG
43267-89QWR-TYPDF-GHJKX-CVBMG
生成每个组合脚本之后,必须插入表格代码。我尝试使用它,但它只会生成该数组的所有组合,而不会生成具有25个simbol的代码。请帮我
<?php
require 'config.php';
function pc_permute($items, $perms = array()) {
if (empty($items)) {
$result = join('', $perms);
mysql_query("INSERT INTO gen (code) VALUES ('$result');") or die ('Wystąpił błąd w zapytaniu lub kod nie zostal dodany.');
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
pc_permute($newitems, $newperms);
}
}
}
$arr = array("2", "3", "4", "6", "7", "8", "9", "Q", "W", "R", "T", "Y", "P", "D", "F", "G", "H", "J", "K", "X", "C", "V", "B", "M");
pc_permute($arr);
?>
如果您能帮助我,我将非常高兴。
PS。请添加声明在一个水泥中不能有5个相同的辛博尔
例如:
AAAAB-AAAAB-AAAAB-AAAAB-AAAAB良好组合
AAAAA-AAAAA-AAAAA-AAAAA-AAAAA错误组合
AAAAA-AAAAB-AAAAB-AAAAB-AAAAB错误组合
最佳答案
function generateId($arr, $max) { //$max sets the amount of characters you want it to be
$results = '';
for($i = 0;$i < $max;$i++) {
if($i % 5 == 0 && $i > 0) { //appends '-' every 5 characters
$results .= '-';
}
$rand = rand(0, (count($arr) - 1));
$results .= $arr[$rand];
}
if(preg_match('/(.)\\1{4}/', $results)) return generateId($arr, $max); //checks if 5 characters repeat
else return $results;
}
$id = generateId($arr, 25); //pass your array of characters as argument