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

Solved Add a string to a config array?

Comments in 'Plugin Development' started by Legoboy0215, Oct 28, 2015.

  1. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    I have this now:
    PHP:
    $config->set("legoboy", array());
    How would I add a value to the array?
  2. zombie_Power
    Offline

    zombie_Power Active Member

    Joined:
    Sep 7, 2015
    Posts:
    239
    Minecraft User:
    Zombie_Power
    Can you please be a little more specific, so I can help you :)
  3. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    It is quite specific. I want to add a string to the array, like an array_push function, but the problem is that IDK how to do that.
    So simply, the config will look like this at first:
    Code:
    legoboy: []
    And it will be changed after I add a string to the array:
    Code:
    legoboy:
    - string
    Thanks!
  4. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    My target? Read my last post. From an empty array to a array with a string.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP:
    $array $config->get("Lego", []); // [] is to make the default value an empty array
    $array[] = "string";
    $config->set("Lego"$array);
    Legoboy0215 likes this.
  6. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    What if I want to add another value?
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Do the same thing. Why do you think anything would be changed?

    BTW, alternative (but I don't like it personally because it won't work with cached getNested):
    PHP:
    $array =& $config->get("stuff");
    $array[] = "string";
    // done! No need to set again because Config::get() returns by reference and you stored variable by reference.
    Legoboy0215 likes this.
  8. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Wow! I learned new stuff.
  9. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Code:
    PHP:
    $array $this->ak->lol->get($player->getName(), []); // [] is to make the default value an empty array
                                    
    $array[] = $this->name;
                                    
    $this->ak->lol->set($player->getName(), $array);
                                    
    $this->ak->lol->save();
    Error: Cannot use a scalar value as an array
  10. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    or
    PHP:
    $config->set("legoboy", array("string""string1""string2"));
  11. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    I want to ADD a string to the config array, not just a default one.
  12. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    sure sorry xD
    Legoboy0215 likes this.
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Tty doing var_dump($array); I want to know what it is. Maybe your config is wrong?
    Legoboy0215 likes this.
  14. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    My .yml file:
    PHP:
    legoboy: []
  15. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    You dont need to save it each time. Using getNested (cached) would be faster. (little bit)
  16. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    How would I do that?
  17. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    use same as $config->setNested() and $config->getNested() instead of $config->get() and $config->set();
    but remember it's cached to you must save it after session.
  18. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    What is a nested value? So
    legoboy: []

    And I do:
    $config->setNested(/*???*/);
  19. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    $config->setNested($k, $v);
    $config->save();
  20. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Would it add a value or replace it?

Share This Page

Advertisement