Random ID/Number Generator in PHP

前端 未结 4 1479
伪装坚强ぢ
伪装坚强ぢ 2021-01-17 05:29

I am building a list of \"agent id\'s\" in my database with the following requirements:

  1. The ID must be 9 digits long (numeric only)
  2. The ID may not con
4条回答
  •  醉酒成梦
    2021-01-17 06:19

    The first solution that comes to mind is a recursive function that simply tests your three requirements and restarts if any three of them fail. Not the most efficient solution but it would work. I wrote an untested version of this below. May not run without errors but you should get the basic idea from it.

    function createRandomAGTNO(){
      srand ((double) microtime( )*1000000);
      $random_agtno = rand(100000000,900000000);
    
      $random_agtno_array = explode('', $random_agtno);
    
      foreach($random_agtno_array as $raa_index => $raa){
        if($raa == $random_agtno_array[$raa_index + 1] && raa == $random_agtno_array[$raa_index + 2]) createRandomAGTNO();
    
        $dup_match = array_search($raa, $random_agtno_array);
        if($dup_match){
          unset($random_agtno_array[$dup_match]);
          if(array_search($raa, $random_agtno_array)) createRandomAGTNO();
        };
      }
    
      return $random_agtno;
    }
    

提交回复
热议问题