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

Problem

Comments in 'Plugin Development' started by MyNameIsTriXz, Feb 15, 2016.

  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    I do $level = $player->getLevel()->getFolderName(), it returns "TW-1"

    When I want to do on
    $games = ["Game1" => ["Arena" => "TW-1", "Phase" => "Joinable", "RedScore" => 0, "BlueScore" => 0]];

    array_search($level, $games), it returns null :| It normally should return "Arena", but why it wont? :/ I dont know what to do anymore...
  2. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    I found a way to fix it by myself, I ll never trust array_search again :p (but please explain me why it did not work with array_search):

    PHP:
    foreach($this->games as $game => $value){
    if(
    $value["Arena"] == $player->getLevel()->getFolderName()){
    return 
    $game;
    }}
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    array_search() doesn't search recursively. If you search with $games["Game1"] instead, it works.
    However, it appears that you only want the Arena key, instead of trying to search through the whole array. So, instead of searching also the Phase and other fields, try this:
    PHP:
    $arenas array_map(function($array){
      
    // $array == ["Arena" => "TW-1", "Phase" => "Joinable", ...]
      
    return $array["Arena"];
    }, 
    $games);
    // $arenas is a copy of $games, but each element now only consists of its "Arena" element.
    array_search($level$games);

Share This Page

Advertisement