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

Solved Implode error help

Discussion in 'Development' started by Levi, Oct 12, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    code
    PHP:
    foreach($this->getServer()->getOnlinePlayers() as $players){
                        
    $playersName $players->getName();
            
    $sender->sendMessage("There are " count($this->getServer()->getOnlinePlayers()) . "/".$this->getServer()->getMaxPlayers(). " players online.\n" implode(", "$playersName));
                    }
    error
    Code:
    [19:08:33] [Server thread/CRITICAL]: Unhandled exception executing command 'list' in list: implode(): Invalid arguments passed
    [19:08:33] [Server thread/CRITICAL]: ErrorException: "implode(): Invalid arguments passed" (EXCEPTION) in "plugins/test/src/test/Main" at line 109
     
  2. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    First thing, remove the sendMessage function from the foreach loop, as that would've sent the player tons of messages(if there wasn't an error with implode)

    Second, you used the implode function wrong. The implode functions calls for an array, and you kept setting a string and then provided a string as an input.

    PHP:
    $playersNames "";
    foreach(
    $this->getServer()->getOnlinePlayers() as $player) {
            
    $playersNames .= $player->getName() . ", "//This attaches the $player->getName() and comma to the end of the $playersNames string.
    }
     
    $sender->sendMessage("There are " count($this->getServer()->getOnlinePlayers()) . "/"$this->getServer()->getMaxPlayers() . " players online.\n" $playersNames);
    If you're really keen on using the implode function:

    PHP:
    $playersNames = [];
    foreach(
    $this->getServer()->getOnlinePlayers() as $player) {
            
    $playersNames[] = $player->getName(); //Adds the player's name to an array
    }
     
    $sender->sendMessage("There are " count($this->getServer()->getOnlinePlayers()) . "/"$this->getServer()->getMaxPlayers() . " players online.\n" implode(", "$playersNames));
     
    Levi likes this.
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    th
    thanks a ton!
     
  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.