faqts : Computers : Programming : Languages : PHP : kms : General : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 10 people (80%) answered Yes
Recently 5 of 7 people (71%) answered Yes

Entry

How can I select 3 different random elements from a single dimension array using rand?

Feb 3rd, 2001 00:04
Philip Olson, Dave Martindale, http://www.php.net/manual/en/function.array-rand.php


The "best" (easiest) way to do this would be to use array_rand, this 
can be done as such :
    <?php
        $array = array('a','b','c','d','e','f','g','h','i','j','k','l');
        srand ((double) microtime() * 1000000); 
        $key = array_rand($array,3); 
        $array[$key[0]]; 
        $array[$key[1]]; 
        $array[$key[2]]; 
    ?>
See :
    http://www.php.net/manual/en/function.array-rand.php