mercredi 27 juillet 2016

php alphanumeric id with fixed letter position

I need to create unique alphanumeric IDs, 6 characters long in PHP.

Although I have found a lot of answers solving this problem, I would like the letters to be positioned in specific place in the ID, for example A1B2C3 (first, third and fifth character).

My only solution for this is to create 6 "for" loops (a-z and 0-9 * 3 times) and insert the output in an array and then in MySQL table. There will definitely be no duplicates but is there any better way?

My code so far is:

$id=array();

 for($a='A';$a!='AA';$a++){
  for($b=1;$b<=9;$b++){
   for($c='A';$c!='AA';$c++){
    for($d=1;$d<=9;$d++){
     for($e='A';$e!='AA';$e++){
      for($f=1;$f<=9;$f++){

        $id[]=$a.$b.$c.$d.$e.$f;
}}}}}}

foreach ($id as $value) {
echo "$value n";
}
?> 

Aucun commentaire:

Enregistrer un commentaire