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

Error in using foreach function

Discussion in 'Development' started by byyEmirhanWSD, Jul 23, 2018.

  1. byyEmirhanWSD

    byyEmirhanWSD Witch

    Messages:
    50
    GitHub:
    EmirhanWSD
    Hi guys. I dont understand this error. Anyone help me?

    PHP:
    public function getArenaPlayer(string $arenaName){
       
    $arenaConfig = new Config($this->getDataFolder()."Arenas/$arenaName.yml"Config::YAML);
       
    $arenaPlayers $arenaConfig->get("Players");
       
    $players = array();
       foreach (
    $arenaPlayers as $player) {
         
    $newPlayer Server::getInstance()->getPlayer($player);
         if(
    $newPlayer instanceof Player){
           
    $players[] = $player;
         }else{
           
    $this->removeArenaPlayer($arenaName$player1);
         }

       }
       return 
    $players;
    }
     
  2. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Send us the error
     
    Muqsit and corytortoise like this.
  3. byyEmirhanWSD

    byyEmirhanWSD Witch

    Messages:
    50
    GitHub:
    EmirhanWSD
    Invalid argument supplied for foreach
     
  4. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Send me the config file. I am pretty sure you cannot foreach that. You could use arrays, but we are trying to fix an error here, not to prevent it by trying something else that may be better.
     
  5. byyEmirhanWSD

    byyEmirhanWSD Witch

    Messages:
    50
    GitHub:
    EmirhanWSD
    ---
    Status: Lobby
    StartTime: 61
    EndTime: 16
    Team: 2
    PlayersPerTeam: 1
    Players: []
    BLUE:
    X: -329
    "Y": 21
    Z: -347
    RED:
    X: -307
    "Y": 17
    Z: -343
    Lobby:
    X: -315
    "Y": 35
    Z: -354
    Yaw: 179.332642
    Pitch: 2.059753
    World: ew
    World: ew
    ...
     
  6. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Players has to look like:
    Players:
    - Player1
    - Player2
    - Player3
    You know what I mean.
     
  7. Eren5960

    Eren5960 Spider Jockey

    Messages:
    27
    GitHub:
    Eren5960
    PHP:
    public function getArenaPlayer(string $arenaName){
       
    $arenaConfig = new Config($this->getDataFolder()."Arenas/$arenaName.yml"Config::YAML);
       
    $arenaPlayers $arenaConfig->get("Players");
       foreach (
    $arenaPlayers as $player => $fix) {
         
    $newPlayer Server::getInstance()->getPlayer($player);
         if(
    $newPlayer instanceof Player){
           
    $arenaPlayers[] = $player;
           
    $arenaConfig->set("Players",$arenaPlayers);
         }else{
           
    $this->removeArenaPlayer($arenaName$player1);
         }

       }
       return 
    $arenaPlayers;
    }
     
  8. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You don't have to have "=> $value" in a foreach loop. Since OP hasn't specified the array in question being associative, it's unnecessary. Since the error says invalid argument supplied, it implies the input is the problem, not the output.
    It would be surprising for me to be right twice in one thread, but I believe [] is proper array syntax for Yaml files, according to this source(May have to scroll to find it). You are right that the array needs to have an element to pass correctly. The solution depends on how OP wants the plugin to function.
    Are you running this function before or after you add player names to the config? If it's after, send the code where you do that. If not, you need to either only run this code when the Arena has players in it, or use is_array to check before, or just cast the data using
    PHP:
     foreach((array) $arenaPlayers as $player) {
     
    xXNiceAssassinlo YT and Muqsit like this.
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Off-Topic: I think you need to use Server::getPlayerExact() instead, for your case.
     
  10. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    You mean Server::getInstance()->getPlayerExact("name")
     
  11. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    actually no
    class::method just meant as a way to tell other the class and the method
    you are never ever suppose to copy and paste that then use it as if it's code

    while yes it's also a way to invoke a STATIC method
    in this context we are just pointing out the class+method
     
    Muqsit likes this.
  12. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
  13. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Besides the above comments, I'm still wondering why you are saving players names to a config file at all. Is the list of players in an arena supposed to survive a server restart?
     
    corytortoise 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.