Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how pick random values from array and send them to other array

Comments in 'Plugin Development' started by Kyoyuki, Dec 13, 2015.

  1. Kyoyuki
    Offline

    Kyoyuki New Member

    Joined:
    Aug 10, 2015
    Posts:
    6
    For example i have:

    Code:
    array(
        0  => value,
        1 => value1,
        2 => value2,
        ...
    )
    
    How can I pick 2 random values from the original array and send them to a new array ,and other values left will be send to other different array.

    So the output I should have 2 array like this:

    Code:
    array(
        0  => value,
        2 => value2,
    )
    
    Code:
    array(
        1  => value1,
        ...
    )
    
    Last edited: Dec 13, 2015
  2. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    PHP:
    $array = array(
    => value,
    => value1,
    => value2
    );
    foreach(
    array_rand($array,2) as $key){//get 2 random keys and copy key and value to new array
    $newarray[$key] = $array[$key];
    unset(
    $array[$key]);
    }
    $newarray2 $array//copy remaining key and values
    PEMapModder likes this.

Share This Page

Advertisement