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
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_searchif(array_search(strtolower($playerName), strtolower($getArray)) == true){ // i'm using 'if' to find whether the search is true or not // code to be execute}
You can use this. PHP: $buildPlayer = [];$name = $player->getName();if(isset($buildPlayer[$name])){ //found} else { //not found}
http://php.net/manual/en/function.array-search.php PHP: mixed array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
PHP: array_map("strtolower", $array); Or if you want to change the keys to lowercase: PHP: array_change_key_case($array, CASE_LOWER);
http://php.net/manual/en/function.strtolower.php Code: string strtolower ( string $string ) You can't pass array in strtolower!
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.
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.
I didn't say anything about array_key_exists... Anyway, one normally wouldn't want to store a null value in the array.
isn't there like an in_array? PHP: $array = ["Bob", " Sarah"];if(in_array("Bob", $array)){ // Found in the array}
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)).