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

How to get an array from config

Discussion in 'Development' started by Atomization, Jun 6, 2018.

  1. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    Hey, how do i get an array from config and return all the the arrays values?
    I have created an array of items and now i want to get them all from the array.

    this code is being used inside on an onCommand function wich is why there is $args[1]
    PHP:
    $items_array $kitsDB->getNested($args[1].".items");
    foreach(
    $items_array as $array){
        if(!
    $array){
            
    //if there are no values in config; set one ;)
            
    $kitsDB->setNested($args[1], ["items"=>$args[2]]);
            
    $kitsDB->save();
        }
    }
    But i get this error:

    PHP:
    Unhandled exception executing command 'kit additem testKit 268 item_name' in kitInvalid argument supplied for foreach()

    ErrorException"Invalid argument supplied for foreach()" (EXCEPTIONin "__SuperKits/src/SuperKits/Main" at line 73
     
  2. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    It depends what data provider you use. Each kind of data provider stores things in different formats.

    Here are some examples of storing an array in some providers.

    YAML
    Code:
    items:
        - 1
        - 2
        - 3
    
    JSON
    Code:
    {
        "items": [
            1,
            2,
            3
        ]
    }
    
     
  3. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    im using yaml
    how about helping me with that error.
     
    Levi likes this.
  4. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    Here's how I would write it.

    PHP:
    $items $config->getNested($args[1].".items");
    foreach(
    $items as $item) {
        if(
    $item !== $args[2]) {
            
    $config->setNested($args[1].".items"$args[2]);
            
    $config->save();
        }
    }
    Idk why'd you use

    PHP:
    if(!$array)
    To my knowledge, I'm pretty sure it is used to check if something is the opposite of a statement. Unless the variable that was in the code was a boolean, I really didn't know what you were doing.
     
  5. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    A piece of advice, it's always good to define variables clearly:). It wouldn't make sense to state

    PHP:
    foreach($items_array as $array) {
     
  6. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    i used if(!$array) to check if there were no items in the array, but i still get this error from before:

    PHP:
    Unhandled exception executing command 'kit additem testKit 268 item_name' in kitInvalid argument supplied for foreach()

    ErrorException"Invalid argument supplied for foreach()" (EXCEPTIONin "__SuperKits/src/SuperKits/Main" at line 73
     
  7. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    You would do
    PHP:
    if($items !== null)
    Send me your full code.
     
  8. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    The reason i haven't just pasted my code and said, "whats the problem," or "Fix it for me" is because I want to actually learn how to fix the one error i had and not rewrite the whole plugin. And the 2nd reason, its in heavy development from trying out different things, however, I have simplified the code to the best of my ability as to your request....

    the idea behind this plugin is to create custom kits, i know about all the other kit plugins but i want this to be my own and give a unique touch to my server. this is the code that creates the kits, there is no code that gives the used the kit (YET)
    PHP:
    public function onCommand(CommandSender $playerCommand $cmd$label, array $args) :bool{
            switch(
    $cmd->getName()){
                case 
    "kit":
                    
    $kitsDB = new Config($this->getDataFolder()."kits.yml"Config::YAML);
                    
    $kitsDB->save();

                    if(!empty(
    $args[0])){
                        if(
    $player->hasPermission("kit.admin")){
                            
    //there is if($args[0] == "create") that creates the kit, and that works fine so i removed it from this post
                            
    if($args[0] == "additem"){
                                if(!empty(
    $args[1])){
                                    if(
    in_array($args[1], $this->kits)){

                                        
    $items_array $kitsDB->getNested($args[1].".items");
                                        foreach(
    $items_array as $array){ //line 73 as shown in the error code
                                            
    if($array !== null){
                                                
    //if the item is not in the array, add it
                                                
    $kitsDB->setNested($args[1], ["items"=>$args[2]]);
                                                
    $kitsDB->save();
                                            } else {
                                                
    //if item is already in array, push a new item in the array
                                                
    array_push($array$args[2]);
                                                
    $kitsDB->setNested($args[1], ["items"=>$args[2]]);
                                                
    $kitsDB->save();
                                            }
                                        }

                                        
    $player->sendMessage(color::BLUE."Added Item with id ".color::RED.$args[2].color::BLUE." to kit ".color::RED.$args[1]."!");

                                    } else {
                                        
    $player->sendMessage(color::RED."Error ".color::BLUE."That kit could not be found!");
                                    }
                                } else {
                                    
    $player->sendMessage(color::RED."Usage:".color::BLUE"/kit additem <kit name> <item id> <amount> <custom name>");
                                }
                            }
                        } else {
                            
    $player->sendMessage(color::RED."You cannot use Admin Commands!");
                        }
                    } else {

                            
    $kits_list implode(" | "$this->kits);

                            
    $player->sendMessage(color::GOLD."-------->".$this->prefix.color::GOLD."<--------");
                            
    $player->sendMessage("/kit [kit name]");
                            
    $player->sendMessage("Avalible kits: ".color::BLUE."[".$kits_list.color::BLUE."]");
                            
    $player->sendMessage(color::GOLD."-------->".$this->prefix.color::GOLD."<--------");
                        }
                return 
    true;
            }
        }
    And this is to reference what the kits.yml file looks like
    PHP:
    ---
    testkit:
       - 
    "264" //onCommand /kit additem is supposed to add a new item to the array
       
    "267"
    testkit2:
       - 
    "62"
    ...

    and this is the error that i want to fix:
    PHP:
    Unhandled exception executing command 'kit additem testKit 268 item_name' in kitInvalid argument supplied for foreach()

    ErrorException"Invalid argument supplied for foreach()" (EXCEPTIONin "__SuperKits/src/SuperKits/Main" at line 73
     
  9. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    Your file should look like
    Code:
    testkit:
     items:
        - 1
        - 2
        - 3
    
     
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    The error is at line 73. Seems like $items_array is not iteratable. Just before line 73, add the following
    PHP:
    var_dump($items_array);
    What shows up in the console before the error?
     
  11. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    you are correct, my mistake. i also found a workaround so i don't need to use a foreach
     
  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.