Simulate an attack while+array+objects
<pre><code>class Gunner {

    public $health;
    public $attack;

    function __construct($health, $attack) {
        $this-&gt;health = $health;
        $this-&gt;attack = $attack;
    }
}

$player = array();
$player[] = new Gunner(100, 20);
$player[] = new Gunner(100, 20);
$player[] = new Gunner(100, 20);

$enemy = array();
$enemy[] = new Gunner(100, 20);
$enemy[] = new Gunner(100, 20);
</code></pre>

<p>I want to have some while loop that run through as long as the both arrays has "entities" / objects. How do I do that?
I want to fight each and every entity, like $player[0] would be fighting (aka do a rand(1,20)) and then remove from the opposites health until its 0. And when its 0 or less I  would remove the entity (object) from the array.</p>

<p>I am not sure how the while loop or the delete from array would look like.</p>

<pre><code>while ((count($attacker) &gt; 0) &amp;&amp; (count($defender) &gt; 0))
{
    $attacker_attack = rand(1, 25);

    $defender[0]-&gt;health -= $attacker_attack;

    if (!$defender[0]-&gt;IsAlive()) {
        unset($defender[0]);
        array_values($defender);
    }

    $defender_attack = rand(1, 20);

    $attacker[0]-&gt;health -= $defender_attack;

    if (!$attacker[0]-&gt;IsAlive()) {
        unset($attacker[0]);
        array_values($attacker);
    }
}
</code></pre>
