Reputation: 97
I want to create alphabetic series in php. The series I want to create is something like:
AAAA
AAAB
AAAC
.
.
.
.
.
.
.
ZZZY
ZZZZ
I do not want any numbers. Pure alphabetic series. What approach can I use to create this? Any hints or pseudo code would be helpful.
I have already gone through range function
and using ascii
values to print alphabets but still could not find a good approach to create such a series.
Upvotes: 1
Views: 914
Reputation: 13313
You can also use while
. PHP can increment characters!!
$letter = 'AAAA';
while (($letters[] = $letter++) !== 'ZZZZ') {}
print_r($letters);
Upvotes: 4
Reputation: 42935
Four nested loops iterating over the set of all valid characters. Then all you have to do is concatenate the current character of each loop to a string:
<?php
$chars = range('A', 'Z');
$words = [];
foreach ($chars as $char1) {
foreach ($chars as $char2) {
foreach ($chars as $char3) {
foreach ($chars as $char4) {
$words[] = $char1 . $char2 . $char3 . $char4;
}
}
}
}
var_dump($words);
The output obviously is:
array(456976) {
[0] =>
string(4) "AAAA"
[1] =>
string(4) "AAAB"
[2] =>
string(4) "AAAC"
[3] =>
string(4) "AAAD"
[4] =>
string(4) "AAAE"
[5] =>
[...]
Upvotes: 1
Reputation: 710
You can use this coding:
<?php
for ($char = 'AAAA'; $char <= 'Z'; $char++) {
echo $char . "\n";
}
?>
Upvotes: 5