Last active
February 25, 2022 09:08
-
-
Save ruman/e9c5936e6b62cce7147717777d143b17 to your computer and use it in GitHub Desktop.
Generate defined ranges of keys to generate unqiue array list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* e.g. A=1 - B=2 - Z=26 - AA=27 - CZ=104 - DA=105 - ZZ=702 - AAA=703 | |
*/ | |
function stringtointvalue($str) | |
{ | |
$amount = 0; | |
$strarra = array_reverse(str_split($str)); | |
for ($i = 0; $i < strlen($str); $i++) { | |
$amount += (ord($strarra[$i]) - 64) * pow(26, $i); | |
} | |
return $amount; | |
} | |
function positionalcomparison($a, $b) | |
{ | |
$a1 = stringtointvalue($a); | |
$b1 = stringtointvalue($b); | |
if ($a1 > $b1) return 1; | |
else if ($a1 < $b1) return -1; | |
else return 0; | |
} | |
function getcolumnrange($min, $max) | |
{ | |
$pointer = strtolower($min); | |
$output = array(); | |
while (positionalcomparison($pointer, strtolower($max)) <= 0) { | |
array_push($output, $pointer); | |
$pointer++; | |
} | |
return $output; | |
} | |
getcolumnrange('a', 'zzz'); // limit ~18000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment