note 98436 added to function.array-rand
| From: | zenko at osu1 dot php dot net | Date: | Tue, 15 Jun 2010 18:55:12 +0000 |
| Subject: | note 98436 added to function.array-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170152@lists.php.net to get a copy of this message | ||
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;
?>
----
Server IP: 195.221.21.36
Probable Submitter: 78.226.14.12
----
Manual Page -- http://www.php.net/manual/en/function.array-rand.php
Edit -- https://master.php.net/note/edit/98436
Del: integrated -- https://master.php.net/note/delete/98436/integrated
Del: useless -- https://master.php.net/note/delete/98436/useless
Del: bad code -- https://master.php.net/note/delete/98436/bad+code
Del: spam -- https://master.php.net/note/delete/98436/spam
Del: non-english -- https://master.php.net/note/delete/98436/non-english
Del: in docs -- https://master.php.net/note/delete/98436/in+docs
Del: other reasons-- https://master.php.net/note/delete/98436
Reject -- https://master.php.net/note/reject/98436
Search -- https://master.php.net/manage/user-notes.php