Kingfox Kingfox - 2 months ago 11
PHP Question

delete item in an array of arrays

First I make two arrays with players and two arrays with their weapons. Then i would like to make a loop where people gets "killed" and remove them from the temporary ($team1 and $team2) array.

Inside the loop it picks a random player from each loop and compares a reaction value and the player with the highest kills the other. It looks like it only runs once, because there is only one line of who kills who in $this->roundevent and the text of who won the round with the score.

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;
}

}


I have tried many different functions to remove a value from the array, but cant seem to figure out whats going on here. (Also i have noticed that i should use the array_rand() function to choose player instead)

Answer

There are multiple way to remove an element from an Array

unset(ARRAY)

$myArray = ['a', 'b', 'c'];

unset($myArray[1]);

output:

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

array_splice(ARRAY, OFFSET, LENGTH)

array_splice($myArray, 1, 1);

or you can use array_diff(), if you know the values of the array.

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

See for more about array_diff()