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

Solved Halp with array indexes

Comments in 'Plugin Development' started by xBeastMode, Dec 30, 2015.

  1. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    I cannot get index from array using string, the only way I can get a index is by doing it with numbers for example array[0], array[1], etc.. If I check if the index with isset(array[index]) or array_key_exists(index,array) it will NOT work, but isset(array[index]) and array_key_exists(number,array) work.
  2. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Code?
    For example:
    PHP:
    <?php

    $array 
    = array('lol' => 1'hoho' => null);
    var_dump(isset($array['lol'])); //True
    var_dump(isset($array['hoho'])); //False

    var_dump(array_key_exists('lol'$array)); //True
    var_dump(array_key_exists('hoho'$array)); //True

    Vaivez66 and PEMapModder like this.
  3. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,397
    Plugins:
    14
    Are you encasing the indices in quotation marks? If so, could you show the code where you define them?

    Note: The plural of index is indices, not indexes.
    PEMapModder likes this.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    and then I started looking at indice.
  5. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    I did this.
    PHP:
    if(!isset($this->waiting[$sign->getText()[1]])) {
                    
    //setting string index in array
                    
    $this->waiting[] = $sign->getText()[1];//way 1
                    
    $this->waiting[$sign->getText()[1]] = $sign->getText()[1];//way 2
                
    }
                
    HGGame::tpToOpenSlot($e->getPlayer(), $sign->getText()[1]);//teleports player to open game in hg
                
    if(isset($this->waiting[$sign->getText()[1]]) || array_key_exists($sign->getText()[1], $this->waiting)) return;//don't work
                
    else{
                    
    $e->getPlayer()->sendMessage(color::RED."This game is already running, please find another game.");
                }
  6. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    $this->waiting[$sign->getText()[1]] will always be set because you set it before checking, so it will always return. What do you mean by dont work? Does it return or send the message?
  7. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Well, I guess the code runs but while checking isset() or array_key_exists() it doesn't find it because it only finds indices numbers like isset(array[0]).
  8. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    No, it will work, like
    PHP:
    $waiting["hi"] = "Hello";
    if(isset(
    $waiting["hi"])) echo $waiting["hi"];
    on the console you will see Hello
    xBeastMode likes this.
  9. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    Why am I so stupid!! #D_O_U_B_L_E_F_A_C_E_P_A_L_M
    It worked, my plugin is almost complete. <^.^>
    PocketKiller likes this.
  10. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    Btw whats the problem?
  11. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    aww.. you did not push anything to the array.. you just made an index.
  12. xBeastMode
    Offline

    xBeastMode Active Member

    Joined:
    Nov 27, 2015
    Posts:
    292
    What problem?

Share This Page

Advertisement