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

Solved Array_push replace old value

Discussion in 'Development' started by BEcraft, Apr 12, 2017.

  1. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    Hello :D
    I'm having little problem with my code xD
    And it's when I try add new player in array it replaces old one, so know someone how can help me?
    This is array for all games:
    PHP:
    public $games = array();
    And this is how I add the player in array:
    PHP:
    public function addPlayer(Player $player$game){
        
    $this->games[$game] = array();
        
    array_push($this->games[$game], $player);   
        }

    //$this->addPlayer($player, $game); in other functions
        
    So it adds fine the first player but when i try to add the second player it replaces the first
    And also made a var_dump (var_dump($this->games[$game]) and here is:
    array(1) {
    [0]=>
    object(pocketmine\Player)#11053 (0) {
    }
    }
    array(1) {
    [0]=>
    object(pocketmine\Player)#17836 (0) {
    }
    }
     
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    PHP:
    $this->games[$game][] = $player;
     
  3. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    Yes I tried this too but still replace old one
     
  4. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    Ups you are right just I forgot to delete $this->games[$game] = array(); xD
    Thanks @Sandertv ! :D
     
    jasonwynn10 likes this.
  5. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    No problem.
     
  6. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    $this->games[$game] is not an array? QAQ
     
  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.