Check is a player in the array

Discussion in 'Development' started by KevTastisch, Apr 7, 2018.

  1. KevTastisch

    KevTastisch Creeper

    Messages:
    4
    Hey Guys!
    I try to make a Build Command and i have the question how can i check is the Player in the variable $buildPlayer? Thanks for answers
    Bye
     
  2. HyGlobalHD

    HyGlobalHD Silverfish

    Messages:
    24
    GitHub:
    HyGlobalHD
    example how to search:
    PHP:
    $getArray = []; // idk if you want from a file or wht but i give ya an array
    $playerName $player->getName();
    // how to search player name in an array:
    // use array_search
    if(array_search(strtolower($playerName), strtolower($getArray)) == true){ // i'm using 'if' to find whether the search is true or not
     // code to be execute
    }
     
  3. LewBr

    LewBr Zombie

    Messages:
    382
    GitHub:
    lewbr
    I think you can't use strtolower on array_search, it needs to be string!
     
  4. AnkitM252

    AnkitM252 Spider Jockey

    Messages:
    29
    GitHub:
    AnkitM252
    You can use this.
    PHP:
    $buildPlayer  = [];
    $name $player->getName();
    if(isset(
    $buildPlayer[$name])){
             
    //found
    } else {
             
    //not found
    }
     
  5. HyGlobalHD

    HyGlobalHD Silverfish

    Messages:
    24
    GitHub:
    HyGlobalHD
  6. Muqsit

    Muqsit Wither Skeleton Verified

    Messages:
    1,383
    GitHub:
    muqsit
    PHP:
    array_map("strtolower"$array);
    Or if you want to change the keys to lowercase:
    PHP:
    array_change_key_case($arrayCASE_LOWER);
     
    AnkitM252 likes this.
  7. AnkitM252

    AnkitM252 Spider Jockey

    Messages:
    29
    GitHub:
    AnkitM252
    http://php.net/manual/en/function.strtolower.php

    Code:
    string strtolower ( string $string )
    
    You can't pass array in strtolower!
     
  8. LewBr

    LewBr Zombie

    Messages:
    382
    GitHub:
    lewbr
    You can't, i have tested on my server and your code gives me an error bout that.
     
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin Noobiest member in the PMMP Team

    Messages:
    1,702
    GitHub:
    sof3
    Associative arrays are stored as hashmaps [citation needed]. It is more efficient to store the name in the key and check if it exists rather than using array_search.
     
  10. yuko fuyutsuki

    yuko fuyutsuki Witch

    Messages:
    68
    GitHub:
    fuyutsuki
    PHP:
    $buildPlayers = [];
    $buildPlayers["example1"] = null;
    $buildPlayers["example2"] = null;
    /**
     * in $bulidPlayers....
     * Array (
     *   ["example1"] => null,
     *   ["example2"] => null
     * )
     */
    $result array_key_exists("example1"$buildPlayers);
    // bool(true)
    As SOF3 says, using isset() is faster than the function overhead by array_key_exists(),
    but this method is recommended because there are pitfalls like the following.
    PHP:
    $example = [
      
    "steve" => null
    ];
    var_dump(array_key_exists("steve"$example));
    // output: bool(true)

    var_dump(isset($example["steve"]));
    // output: bool(false)
    // If the value is not null, true will be returned securely.
     
  11. BLOCKSTORM

    BLOCKSTORM Witch

    Messages:
    61
    I have a build mode plugin, you can message me if you want it
     
  12. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin Noobiest member in the PMMP Team

    Messages:
    1,702
    GitHub:
    sof3
    I didn't say anything about array_key_exists...

    Anyway, one normally wouldn't want to store a null value in the array.
     
    yuko fuyutsuki likes this.
  13. PotatoeTheDev

    PotatoeTheDev Witch

    Messages:
    62
    GitHub:
    potatoetrainyt
    isn't there like an in_array?
    PHP:
    $array = ["Bob"" Sarah"];
    if(
    in_array("Bob"$array)){
        
    // Found in the array
    }
     
  14. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin Noobiest member in the PMMP Team

    Messages:
    1,702
    GitHub:
    sof3
    in_array() is O(n). Although the difference in performance is negligible for this case, it's still more reasonable to use an associative array (which has mostly O(1)).
     
    Muqsit likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.