get random value from a PHP array, but make it unique

后端 未结 9 1113
挽巷
挽巷 2020-12-16 19:44

I want to select a random value from a array, but keep it unique as long as possible.

For example if I\'m selecting a value 4 times from a array of 4 elements, the s

9条回答
  •  粉色の甜心
    2020-12-16 20:38

    I've done this to create a random 8 digit password for users:

    $characters = array(
        "A","B","C","D","E","F","G","H","J","K","L","M",
        "N","P","Q","R","S","T","U","V","W","X","Y","Z",
        "a","b","c","d","e","f","g","h","i","j","k","m",
        "n","p","q","r","s","t","u","v","w","x","y","z",
        "1","2","3","4","5","6","7","8","9");
    
    for( $i=0;$i<=8;++$i ){
        shuffle( $characters );
        $new_pass .= $characters[0];
    }
    

提交回复
热议问题