1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

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 Spider Jockey

    Messages:
    25
    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:
    385
    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 Spider Jockey

    Messages:
    25
    GitHub:
    HyGlobalHD
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    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:
    385
    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

    Messages:
    1,968
    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 Slime

    Messages:
    77
    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:
    67
    I have a build mode plugin, you can message me if you want it
     
  12. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    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. TwistedAsylumMC

    TwistedAsylumMC Slime

    Messages:
    96
    GitHub:
    twistedasylummc
    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

    Messages:
    1,968
    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.
  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.