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

Solved Dealing with (array in) a multidimensional array

Discussion in 'Development' started by Johnmacrocraft, Oct 13, 2018.

  1. Johnmacrocraft

    Johnmacrocraft Silverfish

    Messages:
    24
    GitHub:
    johnmacrocraft
    So my config is something like this (pretty much stripped, but it should be understandable):
    Code:
    ---
    hello:
      test:
        imarray: "value"
        meizarray: []
    ...
    
    What I'm trying to do this is:
    • Check if test (hello.test) exists
    • Get and set value of meizarray

    For them, I tried following codes (once again stripped):
    Code:
    if(in_array(["hello"]["test"], Code::getArray())) {
     //code
    }
    
    Code:
    $testarray = new Config(".../testarray.yml", Config::YAML);
    $key = "hello.test.meizarray";
    $testarray->setNested($key, $testarray->getNested($key)[] = "value");
    $testarray->save();
    
    And yeah, as expected, they didn't do their job.
    How can I make them working?
     
    Primus likes this.
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You should use isset() to check if "test" exists.
    PHP:
    if(isset(Code::getArray()["hello"]["test"])){
        
    //...
    }
    Also, your setNested might be wrong.
    PHP:
    $array = [];
    $value $array[] = "value";
    //$value is "value", and not ["value"]

    //Similarly,
    $array $config->getNested($key);
    $array[] = "value";
    //$array is ["value"], considering $array is [].
    $config->setNested($key$array);
     
    xXNiceAssassinlo YT and Primus like this.
  3. Johnmacrocraft

    Johnmacrocraft Silverfish

    Messages:
    24
    GitHub:
    johnmacrocraft
    Thanks!
     
  4. Johnmacrocraft

    Johnmacrocraft Silverfish

    Messages:
    24
    GitHub:
    johnmacrocraft
    Mind also posting a solution for getting and setting the value of (adding an element to) meizarray? The problem what I'm having with that part is trying to add the element to the array with the code above makes the array to turn into a string. I could add some hacks to convert if it's string, but I don't think that's worth to do for this. Afaik array_push would work too, but I don't want to use that method.
    Edit: Well, nevermind this. Again, thanks for the answer!
     
    Last edited: Oct 13, 2018
  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.