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

Unset a value from an array config

Discussion in 'Facepalm' started by ivanRU, Mar 7, 2017.

  1. ivanRU

    ivanRU Creeper

    Messages:
    5
    Hi
    So i have config:
    ---
    Members:
    - Vasya
    - Petya
    - Itakdalee
    ...
    How can i delete in this array, for example, "Petya"? Help me, please
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    PHP:
    $members $this->getConfig()->get("Members", []);
    unset(
    $members["Petya"]);
    $this->getConfig()->set("Members"$members);
     
    applqpak and corytortoise like this.
  3. ivanRU

    ivanRU Creeper

    Messages:
    5
    OK, but how can i edit this in config?
     
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    See my edited post
     
    jasonwynn10 likes this.
  5. ivanRU

    ivanRU Creeper

    Messages:
    5
    Nope..
     
  6. ivanRU

    ivanRU Creeper

    Messages:
    5
    Members: false
     
  7. ivanRU

    ivanRU Creeper

    Messages:
    5
    Can you show me, how to remove CONSOLE from here (not from Creater) -
    sjs:
    Creater: CONSOLE
    Members:
    - CONSOLE
    - Pie
     
    Last edited: Mar 7, 2017
  8. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    assuming sjs is on the same level as Creater and Members, this code can be used:
    PHP:
    $members $this->getConfig()->get("Members", []);
    unset(
    $members["CONSOLE"]);
    $this->getConfig()->set("Members"$members);
     
  9. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Wow, you just posted the exact same code I posted above, with the name changed to "CONSOLE", congrats :p
     
    Vaivez66 and jasonwynn10 like this.
  10. SavionLegendZzz

    SavionLegendZzz Slime

    Messages:
    75
    GitHub:
    savionlegends
    remember to save the config!
     
  11. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    It works doesn't it? :p
     
  12. ivanRU

    ivanRU Creeper

    Messages:
    5
    Array creating with keys
    Was:
    - CONSOLE
    - Pie
    now:
    1: Pie
     
  13. ivanRU

    ivanRU Creeper

    Messages:
    5
    I hope someone this can help:

    Config:
    ---
    test:
    Members:
    - steve1
    - CONSOLE
    - steve2
    ...
    Code:
    $this->getConfig()->reload();
    $r = "CONSOLE"; //need to remove
    $a1 = "test";
    $array = $this->getConfig()->getAll()[$a1]["Members"]; //members array
    $new = [];
    foreach($array as $value){
    if($value != $r){
    $new[] = $value;}}
    $this->getConfig()->setNested($a1.".Members", $new);
    $this->getConfig()->save();
     
  14. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    Please put your yaml config in a code blocks
    block and php code in a php block.
     
    jasonwynn10 likes this.
  15. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Code:
    ---
    test:
      Members:
      - steve1
      - CONSOLE
      - steve2
    ...
    PHP:
    $this->getConfig()->reload();
    $r "CONSOLE"//need to remove
    $a "test";
    $members $this->getConfig()->getAll()[$a]["Members"]; //members array
    $new = [];
    foreach(
    $members as $member){
      if(
    $member !== $r){
        
    $new[] = $member;
      }
    }
    $this->getConfig()->setNested($a.".Members"$new);
    $this->getConfig()->save();
     
  16. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    PHP:
        public function onEnable() {
            if(!
    file_exists($this->getDataFolder())) {
                @
    mkdir($this->getDataFolder());
                
    $this->saveResource("yourconfig.yml");
            }else
                 if(!
    file_exists($this->getDataFolder()."yourconfig.yml")) {
                
    $this->saveResource("yourconfig.yml");
            }
            
    $this->cfg = new Config($this->getDataFolder()."configname.yml");
            
    $this->getLogger()->info("Enabled !");
        }
        

                 
    //Function to set Members and save
                        
    $name $event->getPlayer()->getName();
                        
    $cfg $this->cfg->get("Members");
                        
    $add['Player'] = $name;
                        
                        
    $i 0;
                        
    $while true;
                        while(
    $while) {
                            if(!isset(
    $cfg[$i])) {
                                
    $cfg[$i] = $add;
                                
    $this->cfg->set("Members",$cfg);
                                
    $this->cfg->save();
                                
    $while false;
                            }else {
                                
    $i++;
                            }
                        }
                    }
     
  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.