Put your tips and tricks online - Share your knowledge! Login | Register
 
 
  Search     Advanced search
 

Home | Ask Question | Add tip | Questions | My tips | Recent tips & tricks | Suggest a category | FAQ | Forums

 
 
 
 Category : Home > Programming > PHP > Strings and Arrays     

Random array element


You can use the following code to print a random element from PHP array:

<?php

$colors = array("red", "blue", "green", "yellow", "orange", "brown", "white", "black");
$colorslen = sizeof($colors) - 1;
$rcolor = $colors[rand(0, $colorslen)];
print $rcolor;

?>


 
   This tip was posted as an answer to question: I want to be able to select a random array element, how do I do it?   
 

  Options
 
   del.icio.us  |  newsvine  |  digg  |  furl  |  google  |  yahoo  |  Ma.gnolia  |  vigillar  |  reddit  |  technorati  |  icerocket  |  pubsub

       Rate this tip:    

Start discussion or add comment to this tip

  Details
Tip reference : #83
views : 619
Added on : 02/17/06
Submited by : h8dk97
 
Send a message Send a message Printer friendly output Printer friendly output
Display this member's tips Display this member's tips (163)
 
 
<< Previous
 Most viewed tips 
  Databases > Oracle > Security : How to unlock Oracle user account  
  Operating Systems > Unix : How to kill Unix user session  
  Databases > Oracle > Performance Tuning : How to enable trace in Oracle  
  Databases > Oracle : Kill user session  
   
  All categories
Databases | Programming | Hardware | Operating Systems | Networking | Internet | ERP / CRM | Games & Multimedia | Graphics & Design | Miscellaneous | Office Software | TipLib FAQ
 
 

Home |  FAQ |  Terms of Use |  Privacy Policy

© 2005 tiplib.com