gpt4 book ai didi

php - MySQL中生成和划分记录的建议

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:58 25 4
gpt4 key购买 nike

我想生成该数组的所有代码组合

"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 个由“-”分隔的段

例如

34267-89QWR-TYPDF-GHJKX-CVBMG

43267-89QWR-TYPDF-GHJKX-CVBMG

生成每个组合脚本后,必须插入到表中的代码。我尝试使用它,但它只生成该数组的所有组合,而不是包含 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);

?>

如果你能帮助我,我会很高兴。

附言。请添加声明在一个sement中不能是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

关于php - MySQL中生成和划分记录的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com