代码之家  ›  专栏  ›  技术社区  ›  Fotis Elmaliotis

固定字母位置的php字母数字id

  •  0
  • Fotis Elmaliotis  · 技术社区  · 8 年前

    我需要创建唯一的字母数字ID,在PHP中有6个字符长。

    虽然我已经找到了很多解决这个问题的答案,但我希望字母能放在ID中的特定位置,例如A1B2C3(第一个、第三个和第五个字符)。

    我唯一的解决方案是创建6个“for”循环(a-z和0-9*3次),并将输出插入数组,然后插入MySQL表。肯定不会有重复,但有更好的方法吗?

    到目前为止,我的代码是:

    <?php
    
       $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";
        }
    ?> 
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   bloodyKnuckles    8 年前

    如果需要顺序ID:

    // to start
    //$numIds = 20000;
    //$id = '';
    
    // to add more
    $numIds = 1000;
    $id = 'A1J5M2'; // put MAX id from DB here
    
    if ( '' !== $id ) { // increment id
      list($aa, $bb, $cc, $dd, $ee, $ff) = str_split($id);
      $arr = [$aa, $bb, $cc, $dd, $ee, $ff];
      list($aa, $bb, $cc, $dd, $ee, $ff) = incc($arr, count($arr) - 1);
    }
    else {
      list($aa, $bb, $cc, $dd, $ee, $ff) = ['A',1,'A',1,'A',1];
    }
    
    // generate ids    
    $ids = [];
    
    for($a=$aa;$a!='AA';$a++){
      for($b=$bb;$b<=9;$b++){
        $bb = 1; // reseting b - f is necessary when adding more ids
        for($c=$cc;$c!='AA';$c++){
          $cc = 'A';
          for($d=$dd;$d<=9;$d++){
            $dd = 1;
            for($e=$ee;$e!='AA';$e++){
              $ee = 'A';
              for($f=$ff;$f<=9;$f++){
                $ff = 1;
    
                $ids[] = "$a$b$c$d$e$f";
                // break out of all loops when desired number is reached
                if ( $numIds <= count($ids) ) { break 6; }
    }}}}}}
    
    // db setup
    $conn = mysqli_connect('localhost', 'root', '', 'dbtest');
    $insertstmt = mysqli_prepare($conn, 'INSERT INTO ids (id) VALUES (?)');
    
    // insert ids
    mysqli_stmt_bind_param($insertstmt, 's', $id);    
    mysqli_autocommit($conn, false);
    foreach ( $ids as $id ) {
      mysqli_stmt_execute($insertstmt);
    }
    mysqli_commit($conn);
    mysqli_close($conn);
    
    
    // function to increment given id, accommodates rolling over, e.g., A9 -> B1
    function incc ($arr, $idx) {
      if ( is_int(intval($arr[$idx])) ) {
        if ( 9 == $arr[$idx] ) {
          $arr[$idx] = 1;
          $arr = incc($arr, --$idx); // recursing
        }
        else {
          $arr[$idx]++;
        }
      }
      else {
        if ( 'Z' === $arr[$idx] ) {
          $arr[$idx] = 'A';
          $arr = incc($arr, --$idx);
        }
        else {
          $ord = ord($arr[$idx]);
          $ord++;
          $arr[$idx] = chr($ord);
        }
      }
      return $arr;
    }
    

    PHP Sandbox demo

        2
  •  0
  •   bloodyKnuckles    8 年前

    对于唯一和随机ID,当您 不需要 顺序ID:

    $numIds = 20000; // how many ids do you want to generate and insert?
    
    // db setup
    $conn = mysqli_connect('localhost', 'root', '', 'dbtest');
    $insertstmt = mysqli_prepare($conn, 'INSERT INTO ids (id) VALUES (?)');
    
    // generate ids array
    $ids = [];
    for ($ii = 0; $ii < $numIds; $ii++) {
      do { // execute loop at least once
    
        // inspired by @chris85 's comment
        $id = randChr() . rand(0, 9) . randChr() . rand(0, 9) . randChr() . rand(0, 9);
    
      } while ( // try again if found in either db or array
        idInDb($conn, $id) || // not actually needed on first run
        (0 < count($ids) && in_array($id, $ids, true))
      );
      $ids[] = $id; // is unique, add to array
    }
    
    // insert ids
    mysqli_stmt_bind_param($insertstmt, 's', $id);    
    mysqli_autocommit($conn, false);
    foreach ( $ids as $id ) {
      mysqli_stmt_execute($insertstmt);
    }
    mysqli_commit($conn);
    mysqli_close($conn);
    
    
    // helper functions
    
    function randChr () { // generate random alpha character
      return chr(rand(65, 90)); // upper case only
      //return chr(rand(0, 1) ? rand(65, 90) : rand(97, 122));
      // use alternative return for upper AND lower case
    }
    
    function idInDB ($conn, $testid) { // check for id in db
      $query = "SELECT id FROM ids WHERE id = '$testid'";
      $result = mysqli_query($conn, $query);
      $found = 0 < mysqli_num_rows($result);
      mysqli_free_result($result);
      return $found;
    }
    

    PHP Sandbox demo