PHP-生成由a-z和a-z组成的n个项目的序列字符数组
我试图用PHP生成一个由n个项目组成的序列字符数组。我想做的是,如果我告诉函数生成第一个,比如说6000个项目,得到如下结果:PHP-生成由a-z和a-z组成的n个项目的序列字符数组,php,arrays,character,sequence,Php,Arrays,Character,Sequence,我试图用PHP生成一个由n个项目组成的序列字符数组。我想做的是,如果我告诉函数生成第一个,比如说6000个项目,得到如下结果: Array ( [0] => a [1] => b [2] => c ... [26] => A [27] => B [28] => C ... [5178] => BaF ) 我已经有了一些函数的起始部分。我可以通过以下方式获得字符范围: array_m
Array (
[0] => a
[1] => b
[2] => c
...
[26] => A
[27] => B
[28] => C
...
[5178] => BaF
)
我已经有了一些函数的起始部分。我可以通过以下方式获得字符范围:
array_merge(range("a", "z"), range("A", "Z"))
我可以生成如下的字符序列:
if (!empty($count)) {
if (is_numeric($count)) {
if ($count > 0) {
$t = $output[] = "a";
for ($i = 0; $i < $count; $i++) {
$output[] = ++$t;
}
}
}
}
if(!empty($count)){
如果(是数字($count)){
如果($count>0){
$t=$output[]=“a”;
对于($i=0;$i<$count;$i++){
$output[]=++$t;
}
}
}
}
这实际上会给我一个从a到z的字符序列,当它达到字符限制时,它会像aa,ab,ac,等等,直到它再次达到限制,然后它会像aaa,aab,aac,等等,等等
如果我替换为$t=$output[]=“a”代码>带有$t=$output[]=“A”代码>除了大写范围外,它的作用相同
这是非常好的,但我想包括大写范围,以及,所以。。。有什么方法可以实现这一点吗?我已经编写了自己的算法来实现您想要的。这很复杂,但我已经尽力在评论中解释了
$chars = array_merge(range("a", "z"), range("A", "Z"));
/*
* You can comfortably change the value of numChars
* to your liking and the code will generate appropriate
* sequence. For example, if you hardcode the value of $numChars
* to 3 then you will get a sequence like so:
* a, b, c, aa, ab, ac, ba, bb, bc, ca, cb, cc, aaa, aab...
*/
$numChars = count($chars);
$output = array();
$count = 6000;
if (!empty($count)) {
if (is_numeric($count)) {
if ($count > 0) {
for ($i = 0; $i < $count; $i++) {
$charPositions = getCharPositions($i, $numChars);
$str = "";
foreach ($charPositions as $pos) {
$str .= $chars[$pos];
}
$output[] = $str;
}
}
}
}
echo "<pre>";
print_r($output);
echo "</pre>";
function getCharPositions($num, $base)
{
/*
* First we find which section the number belongs to
* and we find how many positions it has moved forward in that section
* For example, if you want to loop from a to e then the base will be 5
* if $num is 27 the result is 'ec'
* Since, it has 2 characters it's in the second section
* What it means is that it went from first to last for the first position,
* ie, a to e, and for the second position it moved 22 steps ahead,
* which is basically (27 - (5^1))
*/
$section = 1;
$limit = $base;
while (true) {
$temp = $num - $limit;
if ($temp < 0) {
break;
}
$num = $temp;
$limit *= $base;
$section++;
}
/*
* Once we find out how many steps ahead it has moved in the last section,
* we just need to convert it into a number with the specified base,
* the code below is basically something like converting a decimal number to
* a hexadecimal number, However, each digit of the resultant number is stored
* separately in an array because each of this digit will actually be used as
* position to get the character from the characters array
*/
$positionsFilled = 0;
$result = array();
while ($num > 0) {
$remainder = $num % $base;
$num = (int)($num / $base);
array_unshift($result, $remainder);
$positionsFilled++;
}
/*
* Here we prepend zeros for remaining positions
* because the length of the string should be the
* same as the section it belongs to
*/
while ($positionsFilled < $section) {
array_unshift($result, 0);
$positionsFilled++;
}
return $result;
}
$chars=array_merge(范围(“a”、“z”)、范围(“a”、“z”);
/*
*您可以轻松地更改numChars的值
*根据您的喜好,代码将生成适当的
*顺序。例如,如果您硬编码$numChars的值
*到3,则会得到如下序列:
*a,b,c,aa,ab,ac,ba,bb,bc,ca,cb,cc,aaa,aab。。。
*/
$numChars=计数($chars);
$output=array();
$count=6000;
如果(!空($count)){
如果(是数字($count)){
如果($count>0){
对于($i=0;$i<$count;$i++){
$charPositions=getCharPositions($i,$numChars);
$str=”“;
foreach($charPositions作为$pos){
$str.=$chars[$pos];
}
$output[]=$str;
}
}
}
}
回声“;
打印(输出);
回声“;
函数getCharPositions($num,$base)
{
/*
*首先,我们找到该编号属于哪个部分
*我们发现它在这一部分前进了多少个位置
*例如,如果要从a循环到e,则基数为5
*如果$num为27,则结果为“ec”
*因为它有两个字符,在第二部分
*它的意思是从第一个位置到最后一个位置,
*从a到e,第二个位置向前移动了22步,
*基本上是(27-(5^1))
*/
$section=1;
$limit=$base;
while(true){
$temp=$num-$limit;
如果($temp<0){
打破
}
$num=$temp;
$limit*=基础$base;
$section++;
}
/*
*一旦我们知道它在最后一节中前进了多少步,
*我们只需要把它转换成一个指定基数的数字,
*下面的代码基本上类似于将十进制数转换为
*但是,结果数的每个数字都存储为十六进制数
*在数组中单独使用,因为每个数字实际上都将用作
*从字符数组中获取字符的位置
*/
$positionsFilled=0;
$result=array();
而($num>0){
$resident=$num%$base;
$num=(int)($num/$base);
数组_unshift($result,$restinutes);
$positionsFilled++;
}
/*
*在这里,我们为剩余位置预加零
*因为字符串的长度应该是
*与它所属的部分相同
*/
而($positionsFilled<$section){
数组_unshift($result,0);
$positionsFilled++;
}
返回$result;
}