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

Writing in a YAML file.

Discussion in 'Development' started by xXSirButterXx, Aug 17, 2017.

  1. xXSirButterXx

    xXSirButterXx Witch

    Messages:
    52
    GitHub:
    xxsirbutterxx
    So sometime soon im going to create a plugin that uses a "cmds.yml" folder which i know how to use most of yaml things, except im not totally sure how to writ new keys.
    PHP:
    public function createCmd(string $namestring $cmdint $time){
    $write = [
     
    "$name=>
    [
    "cmd" =>
    [
    "$cmd",
    ]
    "time" => (int)$time,
    ]
    ];
    }
    And i want it to turn out something like
    Code:
    name:
     cmds:
      - cmd 1
      - cmd 2
     time: 2
    
    But im not sure how to do this.
     
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Try using yaml_emit_file(string $file, mixed $data). It'll turn the data into a yaml presentation, AKA what you're trying to do. Note that yaml_emit_file() will replace all content in the file, not add to it. If you'd like to add to it, you could yaml_parse_file(string $file) to get an array containing content from a file, then add data to that array, and finally putting it back using yaml_emit_file(string $file, array $data) again.

    To add things, you could then when you have that data, edit it using array syntax. For example: $data["name"]["cmds"][] = "command";
     
    jasonwynn10 and xXSirButterXx like this.
  3. xXSirButterXx

    xXSirButterXx Witch

    Messages:
    52
    GitHub:
    xxsirbutterxx
    How can i add things like an array.
    For example:
    Code:
    cmd:
     - 1
     - 2
     - 3 # how do i add on to this?
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    $data["cmd"][3] = ...
     
    xXSirButterXx likes this.
  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.