i want to remove the arrary when quit The easy to unset array (may not work) my result: PHP: players: 0blue: 2red: 1green: 0yellow: 0 how i set it: PHP: $this->blue[] = $player->getName(); My code: PHP: public function onQuit(PlayerQuitEvent $e): void{ $player = $e->getPlayer(); $this->unsetarray($player, $this->players); $this->unsetarray($player, $this->blue); $this->unsetarray($player, $this->red); $this->unsetarray($player, $this->green); $this->unsetarray($player, $this->yellow); } public function unsetarray($player, $code){ if(in_array($player->getName(), $this->players)){ unset($this->players[$player->getName()]); } if(in_array($player->getName(), $code)){ unset($code[array_search($player->getName(), $code)]); } }
TIP: when you are storing player objects/names in an array, you should set an identifiable index instead of a value, it'll be easier to remove/get the value later on, use something like this: PHP: $this->blues[spl_object_hash($player)] = $player->getName(); It doesn't have to be spl_object_hash, just something that is "identifiable". Also try passing the var by reference like this: PHP: public function unsetarray($player, array &$code){ if(in_array($player->getName(), $this->players)){ unset($this->players[$player->getName()]); } if(in_array($player->getName(), $code)){ unset($code[array_search($player->getName(), $code)]); } } And if you used my tip: PHP: public function unsetarray($player, array &$code){ if(in_array($player->getName(), $this->players)){ unset($this->players[$player->getName()]); } if(isset($code[spl_object_hash($player)])){ unset($code[spl_object_hash($player)]); } } spl_object_hash docs: http://php.net/manual/en/function.spl-object-hash.php
I had this very same issue a few days ago with array not unsetting player name I simply used: PHP: //To set in arrayif(!in_array($player->getName(), $thearray)){array_push($player->getName(), $thearray);}//To unset from arrayif(in_array($player->getName(), $thearray)){unset($thearray[array_search($player->getName(), $thearray)]);}