note 98435 deleted from function.array-rand by danbrown

From: Date: Tue, 15 Jun 2010 19:40:10 +0000
Subject: note 98435 deleted from function.array-rand by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170154@lists.php.net to get a copy of this message
Note Submitter: zenko ---- unset() seem to me to be a better way to have a unique key in the generated result. Taking 1 from $i can last very long if you generate a long password (try with 20 chars) because array_rand can always "choose" a key which has already been used. <?php $my_array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "0", "1", "2", "3", "4", "5"); $parola = ''; for ($i=0; $i<=15; $i++) { // Generates the random number from the array $random = array_rand($my_array); // Echo the count number and the random number called from array_rand // and print the array to see the last random char has been deleted echo '<br /> count ' . $i . ' => ' . $random . ' call<br />'; print_r($my_array); // Add the char to parola... $parola .= $my_array[$random]; // ...then unset the char from the array unset($my_array[$random]); } // Echo the result echo '<br /><br /> Parola generated : ' . $parola; ?> This is my first code too so please be

« previous php.notes (#170154) next »