note 89159 added to function.array-rand

From: Date: Tue, 24 Feb 2009 15:52:10 +0000
Subject: note 89159 added to function.array-rand
Groups: php.notes 
Request: Send a blank email to php-notes+get-151076@lists.php.net to get a copy of this message
adapted from herodesh's code, will pull a number of unique from arrays. <?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"); for ($i=0; $i<=10; $i++) { $random = array_rand($my_array); //this generates the random number from the array echo "<br> count $i <br> $random call"; // echo the count number and the random number called from array_rand if ($check[$random]<>0) { $i--; echo "ed already try again"; // if check array isnt = 0 then the random number has been already called re random by taking 1 from $i to ensure correct number of results } else { $check[$random]=1; // set check to 1 so it knows the array number has been used $parola .= $my_array[$random];} //here we will display the exact charachter from the array } echo "<br> $parola"; // printing result ?> //delete as required This is my first php script or code or adaption (so be kind),As its most likely bad coding. i have been looking at using php to pull and display random pictures to display on the website not pulling the same picture twice. work obviously required.changing contents of $my_array to "<img src="bla-bla1.jpg>" Basically it just calls an array_rand as in herodesh's code, but uses a second "fake" array to flag true or false (1 or null) to compare if it has been called prior. changing $check[$random]=1 to $check[$random]++1 and if ($check[$random]<>0) to if ($check[$random]<2) obviously changes the number of times it allows the same array to be called randomly. ---- Server IP: 92.48.74.199 Probable Submitter: 78.151.168.235 ---- Manual Page -- http://www.php.net/manual/en/function.array-rand.php Edit -- https://master.php.net/note/edit/89159 Del: integrated -- https://master.php.net/note/delete/89159/integrated Del: useless -- https://master.php.net/note/delete/89159/useless Del: bad code -- https://master.php.net/note/delete/89159/bad+code Del: spam -- https://master.php.net/note/delete/89159/spam Del: non-english -- https://master.php.net/note/delete/89159/non-english Del: in docs -- https://master.php.net/note/delete/89159/in+docs Del: other reasons-- https://master.php.net/note/delete/89159 Reject -- https://master.php.net/note/reject/89159 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151076) next »