[PHP] randomly picking a variable from an array
How do you randomly picking a variable from an array with PHP? Thanks, Joseph PS. Thanks to those who helped me with GD. If you go to my site, you'll see the new counter I created at the top of the page. http://www.collegesucks.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] randomly picking a variable from an array
create a random number from 0 to count($array) and use that as the index jack -Original Message- From: Joseph Bannon [mailto:[EMAIL PROTECTED]] Sent: Friday, September 07, 2001 3:08 PM To: PHP (E-mail) Subject: [PHP] randomly picking a variable from an array How do you randomly picking a variable from an array with PHP? Thanks, Joseph PS. Thanks to those who helped me with GD. If you go to my site, you'll see the new counter I created at the top of the page. http://www.collegesucks.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] randomly picking a variable from an array
Example if you do know the number of items in the array: /* Begin */ $choice = rand(1, 6); echo $array[$choice]; /* End */ That will randomly pick something from 1-6 in the array. If you do not know the number of things in the array (say it is off a form or something): /* Begin */ $numinarray = count($array); $choice = rand(1, $numinarray); echo $array[$choice]; /* End */ HTH! - k - Original Message - From: Joseph Bannon [EMAIL PROTECTED] To: PHP (E-mail) [EMAIL PROTECTED] Sent: Friday, September 07, 2001 3:08 PM Subject: [PHP] randomly picking a variable from an array How do you randomly picking a variable from an array with PHP? Thanks, Joseph PS. Thanks to those who helped me with GD. If you go to my site, you'll see the new counter I created at the top of the page. http://www.collegesucks.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] randomly picking a variable from an array
The easiest way I can think of would be to get a random number based on the size of an array. If you're querying your database and filling an array with all images, just use the database's random routine instead. (I.E. SELECT quote,author from randomQuote ORDER by RAND() LIMIT 1 in mysql) If this isn't the case, something like this should work.. srand(time()); $random=rand(0,sizeof($myVar)-1); Hope I could be of some help... --Justin King, School District of Superior Web Coordinator (www.superior.k12.wi.us) -Original Message- From: Joseph Bannon [mailto:[EMAIL PROTECTED]] Sent: Friday, September 07, 2001 2:08 PM To: PHP (E-mail) Subject: [PHP] randomly picking a variable from an array How do you randomly picking a variable from an array with PHP? Thanks, Joseph PS. Thanks to those who helped me with GD. If you go to my site, you'll see the new counter I created at the top of the page. http://www.collegesucks.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] randomly picking a variable from an array
But that would make sense rasmus... and I don't like to make sense =)... if its an array filled from a database, randomizing on the database level would still be better though. --Justin King, School District of Superior Web Coordinator (www.superior.k12.wi.us) -Original Message- From: Rasmus Lerdorf [mailto:[EMAIL PROTECTED]] Sent: Friday, September 07, 2001 3:13 PM To: King, Justin Cc: Joseph Bannon; [EMAIL PROTECTED] Subject: RE: [PHP] randomly picking a variable from an array I'd say the easiest way would be to use array_rand() See http://php.net/array_rand -Rasmus On Fri, 7 Sep 2001, King, Justin wrote: The easiest way I can think of would be to get a random number based on the size of an array. If you're querying your database and filling an array with all images, just use the database's random routine instead. (I.E. SELECT quote,author from randomQuote ORDER by RAND() LIMIT 1 in mysql) If this isn't the case, something like this should work.. srand(time()); $random=rand(0,sizeof($myVar)-1); Hope I could be of some help... --Justin King, School District of Superior Web Coordinator (www.superior.k12.wi.us) -Original Message- From: Joseph Bannon [mailto:[EMAIL PROTECTED]] Sent: Friday, September 07, 2001 2:08 PM To: PHP (E-mail) Subject: [PHP] randomly picking a variable from an array How do you randomly picking a variable from an array with PHP? Thanks, Joseph PS. Thanks to those who helped me with GD. If you go to my site, you'll see the new counter I created at the top of the page. http://www.collegesucks.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] randomly picking a variable from an array
On Fri, 7 Sep 2001, King, Justin wrote: The easiest way I can think of would be to get a random number based on the size of an array. If you're querying your database and filling an array with all images, just use the database's random routine instead. (I.E. SELECT quote,author from randomQuote ORDER by RAND() LIMIT 1 in mysql) If this isn't the case, something like this should work.. srand(time()); $random=rand(0,sizeof($myVar)-1); Hope I could be of some help... array_rand() will also do this for you... -Sterling --Justin King, School District of Superior Web Coordinator (www.superior.k12.wi.us) -Original Message- From: Joseph Bannon [mailto:[EMAIL PROTECTED]] Sent: Friday, September 07, 2001 2:08 PM To: PHP (E-mail) Subject: [PHP] randomly picking a variable from an array How do you randomly picking a variable from an array with PHP? Thanks, Joseph PS. Thanks to those who helped me with GD. If you go to my site, you'll see the new counter I created at the top of the page. http://www.collegesucks.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] randomly picking a variable from an array
Which is more resource attentive however (Random question, not necessarily pertaining to this)? Making MySQL do extra work or PHP do some extra work? - k - Original Message - From: King, Justin [EMAIL PROTECTED] To: Rasmus Lerdorf [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Friday, September 07, 2001 4:19 PM Subject: RE: [PHP] randomly picking a variable from an array But that would make sense rasmus... and I don't like to make sense =)... if its an array filled from a database, randomizing on the database level would still be better though. --Justin King, School District of Superior Web Coordinator (www.superior.k12.wi.us) -Original Message- From: Rasmus Lerdorf [mailto:[EMAIL PROTECTED]] Sent: Friday, September 07, 2001 3:13 PM To: King, Justin Cc: Joseph Bannon; [EMAIL PROTECTED] Subject: RE: [PHP] randomly picking a variable from an array I'd say the easiest way would be to use array_rand() See http://php.net/array_rand -Rasmus On Fri, 7 Sep 2001, King, Justin wrote: The easiest way I can think of would be to get a random number based on the size of an array. If you're querying your database and filling an array with all images, just use the database's random routine instead. (I.E. SELECT quote,author from randomQuote ORDER by RAND() LIMIT 1 in mysql) If this isn't the case, something like this should work.. srand(time()); $random=rand(0,sizeof($myVar)-1); Hope I could be of some help... --Justin King, School District of Superior Web Coordinator (www.superior.k12.wi.us) -Original Message- From: Joseph Bannon [mailto:[EMAIL PROTECTED]] Sent: Friday, September 07, 2001 2:08 PM To: PHP (E-mail) Subject: [PHP] randomly picking a variable from an array How do you randomly picking a variable from an array with PHP? Thanks, Joseph PS. Thanks to those who helped me with GD. If you go to my site, you'll see the new counter I created at the top of the page. http://www.collegesucks.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] randomly picking a variable from an array
Actually, the difference in speed between generating a random number in either MySQL or PHP is pretty much negligable. If you can have MySQL return just a single row, however, instead of an entire set of rows then there will be a notable difference =) Sheridan Saint-Michel Website Administrator FoxJet, an ITW Company www.foxjet.com - Original Message - From: Kath [EMAIL PROTECTED] To: King, Justin [EMAIL PROTECTED]; Rasmus Lerdorf [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Friday, September 07, 2001 3:57 PM Subject: Re: [PHP] randomly picking a variable from an array Which is more resource attentive however (Random question, not necessarily pertaining to this)? Making MySQL do extra work or PHP do some extra work? - k - Original Message - From: King, Justin [EMAIL PROTECTED] To: Rasmus Lerdorf [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Sent: Friday, September 07, 2001 4:19 PM Subject: RE: [PHP] randomly picking a variable from an array But that would make sense rasmus... and I don't like to make sense =)... if its an array filled from a database, randomizing on the database level would still be better though. --Justin King, School District of Superior Web Coordinator (www.superior.k12.wi.us) -Original Message- From: Rasmus Lerdorf [mailto:[EMAIL PROTECTED]] Sent: Friday, September 07, 2001 3:13 PM To: King, Justin Cc: Joseph Bannon; [EMAIL PROTECTED] Subject: RE: [PHP] randomly picking a variable from an array I'd say the easiest way would be to use array_rand() See http://php.net/array_rand -Rasmus On Fri, 7 Sep 2001, King, Justin wrote: The easiest way I can think of would be to get a random number based on the size of an array. If you're querying your database and filling an array with all images, just use the database's random routine instead. (I.E. SELECT quote,author from randomQuote ORDER by RAND() LIMIT 1 in mysql) If this isn't the case, something like this should work.. srand(time()); $random=rand(0,sizeof($myVar)-1); Hope I could be of some help... --Justin King, School District of Superior Web Coordinator (www.superior.k12.wi.us) -Original Message- From: Joseph Bannon [mailto:[EMAIL PROTECTED]] Sent: Friday, September 07, 2001 2:08 PM To: PHP (E-mail) Subject: [PHP] randomly picking a variable from an array How do you randomly picking a variable from an array with PHP? Thanks, Joseph PS. Thanks to those who helped me with GD. If you go to my site, you'll see the new counter I created at the top of the page. http://www.collegesucks.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] randomly picking a variable from an array
Which is more resource attentive however (Random question, not necessarily pertaining to this)? Making MySQL do extra work or PHP do some extra work? Generally it would be more efficient to have MySQL do it as part of its SELECT. But there could be cases where it would be more efficient to do it in PHP. Really depends on what you are doing. Try benchmarking both. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]