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

Getting info out of an array

Comments in 'Plugin Development' started by TryoneLegendZzz, Sep 17, 2015.

  1. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    ok so i want to get a random player out of an array how might i do this?
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    TryoneLegendZzz likes this.
  3. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
  4. basprohop
    Offline

    basprohop Active Member Plugin Developer

    Joined:
    Aug 8, 2015
    Posts:
    128
    Plugins:
    3
    Minecraft User:
    basprohop
    You don't know basic PHP do you?
    Legoboy0215 likes this.
  5. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    You are using array_rand wrong.
    PHP:
    $array $this->players;
    $randomPlayer array_rand($array10); // You can change 10 to anything
  6. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    i have used array_rand for my cratekeys plugin. so i thought i didnt need to be the same as that. btw do i change 10 to the number of players?
  7. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Did you even look at the link I gave you? Read. http://php.net/manual/en/function.array-rand.php
    Hotshot_9930 likes this.
  8. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    The 10 was just an example.
    You change it to how much entries you want to take from the array.
    In your case that would be 1.
  9. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Que reply: "Would that be the amount of players on the server?"
  10. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Lmao
  11. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Que reply: "Not funny I'm going to ask @shoghicp to ban you!!!"
    Hotshot_9930 likes this.
  12. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Que reply: "It doesn't return a player, it returns a number!"
    Last edited: Sep 17, 2015
    PEMapModder likes this.
  13. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    Oh and if you actually ask it, you get a key from array_rand so you need this: :p
    PHP:
    $key array_rand($this->players1);
    $randomplayer $this->players[$key];
    Primus likes this.
  14. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    .-.
  15. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Just for curiosity - why you need it?
  16. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    i am making a minigame and i want to pick a random player out of an array
  17. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Why you dont do
    PHP:
    $array = array(GithubMicrosoftFacebookTwitter);
    $randValue $array[rand(0count($array))]; // if rand value is 3...
    echo $randValue;
    outputs: 'Twitter'
    TryoneLegendZzz likes this.
  18. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    well i have this
    PHP:
    $this->players = array(
    "var"=> $player->getName()
    );
    and i want to pick a random player and them into
    PHP:
    public $vip = array();
  19. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    I explained how to do it before. I'll explain again:
    PHP:
    $array $this->players;
    $randomPlayer array_rand($array1); // Selects 1 random player from the array
    And to put the random player into $vip:
    PHP:
    array_push($this->vip$randomPlayer);
    Also see how array_push() works here:
    http://php.net/manual/en/function.array-push.php
  20. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    i have done this already. but thanks :D

Share This Page

Advertisement