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

Config unset

Discussion in 'Plugin Help' started by Imao, Dec 23, 2020.

  1. Imao

    Imao Creeper

    Messages:
    4
    Hello,how to unset a config file like the config file look like this


    Family
    -Joe
    -David
    -Marry


    Then I wanna to delete "David",after delete it look like

    Family
    -Joe
    -Marry
     
  2. ethaniccc

    ethaniccc Baby Zombie

    Messages:
    189
    GitHub:
    ethaniccc
    Using Config->get(), you can get the "Family" (which will return an array)
    Then, you will want to remove "David" from the array, here's something from StackOverFlow:
    upload_2020-12-23_10-21-32.png
    upload_2020-12-23_10-20-23.png
    Then, using Config->set() (iirc), set the Family to the new array and save the config
     
    Primus likes this.
  3. Imao

    Imao Creeper

    Messages:
    4
    Ya I try it but It cames to
    Code:
    Family:
    -Joe
    -David
    -Marry
    to this
    Code:
    Family
     1: Joe
     2: David
     3: Marry
     
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    It's actually the same thing once it is parsed. But I understand why you would prefer the first one over the other. While I currently don't have an answer you're looking for. I'm here to spread propaganda.

    I discourage people from actually using Configs as a place to store data. It was never meant for that anyway.

    What I would do is, handle your raw data like you would through arrays, object properties etc. And implement load & save operations.

    for YAML just do:
    PHP:
    # Loading
    $data yaml_parse_file('path/to/file.yaml');
    # Saving
    $data = [
       
    'Family' => ['Joe''David''Marry']
    ];
    yaml_emit_file("path/to/file.yaml"$data);
    for JSON:
    PHP:
    # Loading
    $data json_decode(file_get_contents('path/to/file.yaml'), true);

    # Saving
    file_put_contents(json_encode($dataJSON_PRETTY_PRINT));
    Might seem complicated and unnecessary. But really is not. If you think about it, it's a Configuration file not a database.

    I don't see why would you wrap an array into new class, just to change one value.
     
    Last edited: Dec 26, 2020
    mmm545 and ethaniccc like this.
  5. Imao

    Imao Creeper

    Messages:
    4
    Um... I mean... Like this, example. I want to delete a value David from config.
    Before delete
    Code:
    Family:
    -Joe
    -David
    -Marry
    After delete
    Code:
    Family
    - Joe
    - Marry
    I need it like this...
     
  6. OguzhanUmutlu

    OguzhanUmutlu Witch

    Messages:
    63
    GitHub:
    OguzhanUmutlu
    PHP:
    $old $config->getNested("your.array");
    unset(
    $old[array_search($old"David")]);
    $config->setNested("your.array"$old);
    $config->save();
    $config->reload();
     
  7. Imao

    Imao Creeper

    Messages:
    4
    your.array its what??
     
  8. OguzhanUmutlu

    OguzhanUmutlu Witch

    Messages:
    63
    GitHub:
    OguzhanUmutlu
    config's variable name that you want to save
     
  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.