Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 03-11-2004, 11:42 AM   #1 (permalink)
Epsilon
Regular Contributor
 
Epsilon's Avatar
 
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 131
Epsilon is on a distinguished road
Quick way to select a random array element?

$array = array('red','yellow','blue','green');

What would be a quick, one line way of selecting a random element from an array like this?
__________________
--Epsilon--
Epsilon is offline   Reply With Quote
Old 03-11-2004, 03:20 PM   #2 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
PHP Code:
$array = array('red','yellow','blue','green');

// assumes numerically indexed array
echo $array[rand(0count($array)-1)]; 
bdl is offline   Reply With Quote
Old 03-11-2004, 04:10 PM   #3 (permalink)
Epsilon
Regular Contributor
 
Epsilon's Avatar
 
Join Date: Mar 2003
Location: Las Vegas, NV
Posts: 131
Epsilon is on a distinguished road
Excellent, thanks.
__________________
--Epsilon--
Epsilon is offline   Reply With Quote
Old 03-11-2004, 04:22 PM   #4 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Not at all. ;-)
bdl is offline   Reply With Quote
Old 03-11-2004, 05:49 PM   #5 (permalink)
Stan
Registered User
 
Stan's Avatar
 
Join Date: Mar 2004
Posts: 2
Stan is on a distinguished road
alternatively you could try this:
PHP Code:
<?
$array 
= array('red','yellow','blue','green');

echo 
$arrayarray_rand($array) ];
?>
array_rand() with only the array as an argument will return 1 random key.

the cool thing about the array_rand() function is that it can return more than 1 random key if you give it a second argument. this would be comparable to an sql select statement with a random clause.

so if i wanted 2 random colors from that array, i could do this:
PHP Code:
<?
$array 
= array('red','yellow','blue','green');

$rand_array_keys array_rand($array,2);

echo 
"First Random Color: " $array$rand_array_keys[0] ] . "<br>\n
     Second Random Color: " 
$array$rand_array_keys[1] ];
?>
Might not be that relevant to your situation, but this a way you could assure to grab 2 different random elements from the array.

i forget if it was the sql random, or php random function that didn't always return very random results with a small range of options to choose from.
Stan is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 09:50 AM


All times are GMT -8. The time now is 10:05 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting