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

Solved array not unsetting

Discussion in 'Development' started by xXNiceAssassinlo YT, Aug 12, 2018.

  1. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    i want to remove the arrary when quit
    The easy to unset array (may not work)

    my result:
    PHP:
    players0
    blue
    2
    red
    1
    green
    0
    yellow
    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)]);
            }
        }
     

    Attached Files:

  2. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    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
     
    Muqsit and OnTheVerge like this.
  3. OnTheVerge

    OnTheVerge Spider Jockey

    Messages:
    46
    I had this very same issue a few days ago with array not unsetting player name I simply used:

    PHP:
    //To set in array
    if(!in_array($player->getName(), $thearray)){
    array_push($player->getName(), $thearray);
    }

    //To unset from array
    if(in_array($player->getName(), $thearray)){
    unset(
    $thearray[array_search($player->getName(), $thearray)]);
    }
     
  4. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    xBeastMode. Helped I just needed
    Code:
    array &$code
     
  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.