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

Add a value to an array

Comments in 'Plugin Development' started by AndrewBit, Dec 12, 2015.

  1. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I have a array on my yaml...

    Code:
    array:
    - blabla1
    - blabla2
    - blabla3
    
    How I can add a value to it?
    I want add blabla4.

    EX:
    Code:
    array: 
     - blabla1
     - blabla2
     - blabla3
     - blabla4
    
  2. mamayadesu
    Offline

    mamayadesu Active Member Plugin Developer

    Joined:
    Sep 19, 2013
    Posts:
    146
    Plugins:
    4
    Minecraft User:
    mamayadesu
    Try it:
    PHP:
    $array $this->getConfig()->get('array');
    $new_array $array;
    $new_array[] = 'blabla4';
    $this->getConfig()->set('array'$new_array);
    $this->getConfig()->save();
    AndrewBit likes this.
  3. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Thanks! Now I have another small problem... how I can delete blabla2 (for example)?
  4. mamayadesu
    Offline

    mamayadesu Active Member Plugin Developer

    Joined:
    Sep 19, 2013
    Posts:
    146
    Plugins:
    4
    Minecraft User:
    mamayadesu
    PHP:
    $array $this->getConfig()->get('array'); // values: blabla1, blabla2, blabla3, blabla4
    $for_remove 'blabla2';
    $new_array = [];
    foreach(
    $array as $value) {
        if(
    $value != $for_remove) {
            
    $new_array[] = $value;
        }
    }
    $this->getConfig()->set('array'$new_array);
    $this->getConfig()->save();
    AndrewBit likes this.
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    $new "blabla2";
    $array $this->getConfig()->get("array");
    for (
    $i 0$i <= count($array); $i++) {
    if (
    $array[$i] === $new) {
         unset(
    $array[$i]);
    }
    }
    sort($array);
    $this->getConfig()->setNested("array"$array);
    $this->getConfig()->save();
    AndrewBit likes this.
  6. mamayadesu
    Offline

    mamayadesu Active Member Plugin Developer

    Joined:
    Sep 19, 2013
    Posts:
    146
    Plugins:
    4
    Minecraft User:
    mamayadesu
    don't forget:
    and count() will return 4
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Or simply use array_search() + unset()
    Or just make the data be in the keys instead of the values.
    AndrewBit and mamayadesu like this.
  8. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    It works! But I have a problem...
    I do a command, and it works the first time...
    But the second time it removes the first time value and add the new value.

    I mean:
    /aw add pedro

    And now the yaml shows:
    Code:
    ex:
     - ex
     - ex
     - pedro
    
    But if execute it another time (this time /aw add dro), the yaml shows:
    Code:
    ex:
     - ex
     - ex
     - dro
    
  9. mamayadesu
    Offline

    mamayadesu Active Member Plugin Developer

    Joined:
    Sep 19, 2013
    Posts:
    146
    Plugins:
    4
    Minecraft User:
    mamayadesu
    Show full code.
  10. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    RemovePlayer not working.
    AddPlayer working bad (I said you the problem :'] )

    PHP:
        private function addPlayer($playerName){
                
    $newPlayer $this->allowJoinList;
                
    $newPlayer[] = $playerName;
                
    $this->getConfig()->set("AllowJoin"$newPlayer);
                
    $this->getConfig()->save();
        }

        private function 
    removePlayer($playerName){
            for (
    $i 0$i <= count($this->allowJoinList); $i++) {
                if (
    $this->allowJoinList[$i] === $playerName) {
                    unset(
    $this->allowJoinList[$i]);
                }
            }
    sort($this->allowJoinList);
            
    $this->getConfig()->setNested("AllowJoin",$this->allowJoinList);
            
    $this->getConfig()->save();
        }
    Commands:

    PHP:
        # Commands

        
    public function OnCommand(CommandSender $senderCommand $command$label, array $args){
            if(
    $command->getName() == "aw" and $sender->hasPermission("commands.alwaysjoin")){
                switch(
    strtolower($args[0])){
                    case 
    "add":
                            if (isset(
    $args[1])) {
                                
    $this->addPlayer($args[1]);
                                
    $sender->sendMessage("Added " $args[1] . " to AlwaysJoin :)");
                            } else {
                                
    $sender->sendMessage("Usage: /aw <add/remove> [player]");
                            }

                    break;
                    case 
    "remove":
                        if(isset(
    $args[1])) {
                            
    $this->removePlayer($args[1]);
                        }
                        else{
                            
    $sender->sendMessage("Usage: /aw <add/remove> [player]");
                        }
                    break;
                    default:
                        
    $sender->sendMessage("Usage: /aw <add/remove> [player]");
                        break;
                }
            }
        }
    }
  11. mamayadesu
    Offline

    mamayadesu Active Member Plugin Developer

    Joined:
    Sep 19, 2013
    Posts:
    146
    Plugins:
    4
    Minecraft User:
    mamayadesu
    for remove try my code:
    PHP:
    $array $this->getConfig()->get('array'); // values: blabla1, blabla2, blabla3, blabla4
    $for_remove 'blabla2';
    $new_array = [];
    foreach(
    $array as $value) {
        if(
    $value != $for_remove) {
            
    $new_array[] = $value;
        }
    }
    $this->getConfig()->set('array'$new_array);
    $this->getConfig()->save();
    and why addPlayer working bad? More info, please.
  12. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    When I run the command two times, the first time it works fine, but the second it only delete the first line...
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Use array_search() >.<
    Vaivez66 likes this.
  14. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    $this->getConfig()->reload();
    Bumping threads is cool..

Share This Page

Advertisement