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

How do I set a value of an array in a config?

Comments in 'Plugin Development' started by XFuryMCPE, Jun 27, 2016.

  1. XFuryMCPE
    Offline

    XFuryMCPE Active Member Plugin Developer

    Joined:
    Jun 19, 2015
    Posts:
    137
    Plugins:
    2
    Minecraft User:
    XFuriousMC
    Config look:

    Name:
    value: 1
    value2: 1
    value3: 1

    how do I change value2?
  2. Extreme_Heat
    Offline

    Extreme_Heat Active Member

    Joined:
    Apr 19, 2016
    Posts:
    76
    Minecraft User:
    Extreme_Heat
    applqpak likes this.
  3. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    The way I would do it is like so:

    PHP:
    $array $this->cfg->get("Name");

    $array["value2"]  = "new_value";

    $this->cfg->set("Name"$array);

    $this->cfg->save();
    Buuuutttttt... @Extreme_Heat's answer is probably more efficient.
  4. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    First, that's not an array, second, I personally prefer use @applqpak method.
    applqpak likes this.
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    That would be
    Code:
    name.value1: Hello
    If the config should look like the one you posted, "Name" would be empty. This is a YAML file, make sure to use spaces.
    Code:
    Name:
      value1: Hi
      value2: Bye
    Here, you can/have to use setNested().
    PHP:
    $config->setNested("Name.value1""Hey");
    The dot basically tells the config to go into the next "dimension" of the array. Only at setNested() though.

Share This Page

Advertisement