我想生成该数组的所有代码组合
"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个符号,并且有5个段由&#34组成; - "
例如
34267-89QWR-TYPDF-GHJKX-CVBMG
43267-89QWR-TYPDF-GHJKX-CVBMG
生成每个组合脚本后必须instert到表那个代码。我尝试使用它,但它只生成该数组的所有组合,但不生成25个符号的代码。请帮帮我
<?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。请添加声明在一个sement中不能是相同的simbols
例如:
AAAAB-AAAAB-AAAAB-AAAAB-AAAAB 良好的组合
AAAAA-AAAAB-AAAAB-AAAAB-AAAAB BAD COMBINATION
答案 0 :(得分:3)
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