#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    13
    Rep Power
    0

    Random array Select in Multidimensional Arrays


    I have a multidimensional array:
    PHP Code:
     $arr = array(
        array(
    "a""one"),
        array(
    "b""two"),
        array(
    "c""three"),
        array(
    "d""four"),
        array(
    "e""five"),
        array(
    "f""six"),
        array(
    "g""seven")
    ); 
    i want randomly obtain several values of $arr, like:
    PHP Code:
     $rand_arr = array(
        array(
    "b""two"),
        array(
    "d""four"),
        array(
    "f""six"),
    ); 
    How can do it?
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,183
    Rep Power
    9398
    array_rand to get a random set of keys from $arr, and a foreach loop over that to copy the corresponding values into a new array.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    // Nevermind
    Last edited by Jacques1; January 4th, 2014 at 05:29 AM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo