删除数组数组中的项

时间:2016-10-07 21:44:06

标签: php arrays

首先,我制作了两个带有玩家的阵列和两个带有武器的阵列。然后我想做一个循环,人们被“杀死”并从临时($ team1和$ team2)数组中删除它们。

在循环内部,它从每个循环中选择一个随机玩家并比较反应值和最高杀死另一个玩家。看起来它只运行一次,因为在$ this-> roundevent中只有一行杀死谁,以及用该分数赢得该回合的文本。

function fightRound() {
    unset($this->roundevent);
    $team1 = array($this->t1p1, $this->t1p2, $this->t1p3, $this->t1p4, $this->t1p5);
    $team1wepaons = array($this->t1p1weap, $this->t1p2weap, $this->t1p3weap, $this->t1p4weap, $this->t1p5weap);
    $team2 = array($this->t2p1, $this->t2p2, $this->t2p3, $this->t2p4, $this->t2p5);
    $team2wepaons = array($this->t2p1weap, $this->t2p2weap, $this->t2p3weap, $this->t2p4weap, $this->t2p5weap);


    $alive = true;
    while ($alive) {        

        /* get random team1 player */
        $randomt1 = rand(1, count($team1));

        /* get random team2 player */
        $randomt2 = rand(1, count($team2));


        if ($randomt1 == 1) {
            $playerteam1 = $team1[0];
            $playerteam1w = $team1wepaons[0];
        } else if ($randomt1 == 2) {
            $playerteam1 = $team1[1];
            $playerteam1w = $team1wepaons[1];
        } else if ($randomt1 == 3) {
            $playerteam1 = $team1[2];
            $playerteam1w = $team1wepaons[2];
        } else if ($randomt1 == 4) {
            $playerteam1 = $team1[3];
            $playerteam1w = $team1wepaons[3];
        } else if ($randomt1 == 5) {
            $playerteam1 = $team1[4];
            $playerteam1w = $team1wepaons[4];
        }

        if ($randomt2 == 1) {
            $playerteam2 = $team2[0];
            $playerteam2w = $team2wepaons[0];
        } else if ($randomt2 == 2) {
            $playerteam2 = $team2[1];
            $playerteam2w = $team2wepaons[1];
        } else if ($randomt2 == 3) {
            $playerteam2 = $team2[2];
            $playerteam2w = $team2wepaons[2];
        } else if ($randomt2 == 4) {
            $playerteam2 = $team2[3];
            $playerteam2w = $team2wepaons[3];
        } else if ($randomt2 == 5) {
            $playerteam2 = $team2[4];
            $playerteam2w = $team2wepaons[4];
        }

        if ($playerteam1[reaction] > $playerteam2[reaction]) {
            $this->roundevent .= $playerteam1[id]." kills ".$playerteam2[id]." with ".$playerteam1w;
            $team2 = array_diff($team2, array([$playerteam2]));
        } else if ($playerteam1[reaction] < $playerteam2[reaction]) {
            $this->roundevent .= $playerteam2[id]." kills ".$playerteam1[id]." with ".$playerteam2w;
            $team1 = array_diff($team1, array([$playerteam1]));
        } else if ($playerteam1[reaction] == $playerteam2[reaction]) {
            $whodies = rand(1,2);
            if ($whodies == 1) {
                $this->roundevent .= $playerteam1[id]." kills ".$playerteam2[id]." with ".$playerteam1w;
                $team1 = array_diff($team1, array([$playerteam1]));
            } else {
                $this->roundevent .= $playerteam2[id]." kills ".$playerteam1[id]." with ".$playerteam2w;
                $team2 = array_diff($team2, array([$playerteam2]));
            }
        }


        $this->roundevent .= " <br> ";

        /* check if all players in a team is dead */
        if (empty($team1)) {
            $this->t2score = $this->t2score + 1;
            $this->roundevent .= " <br> Team 2 scores - ".$this->t2score;
            $alive = false;
        } else if (empty($team2)) {
            $this->t1score = $this->t1score + 1;
            $this->roundevent .= " <br> Team 1 scores - ".$this->t1score;
            $alive = false;
        }

    }

我已尝试过许多不同的函数来从数组中删除一个值,但似乎无法弄清楚这里发生了什么。 (我也注意到我应该使用array_rand()函数来选择播放器)

1 个答案:

答案 0 :(得分:1)

有多种方法可以从数组中删除元素

<强>未设置(ARRAY)

$ myArray = [&#39; a&#39;,&#39; b&#39;,&#39; c&#39;];

unset($myArray[1]);

输出:

Array (
  [0] => a
  [1] => c
)

array_splice(ARRAY,OFFSET,LENGTH)

array_splice($myArray, 1, 1);

如果您知道数组的值,则可以使用 array_diff()

$newArray = array_diff($myArray, ["a", "c"]);

有关array_diff()

的详情,请参阅