<?php
$cards = [1,2,3,4,5,6,7,8,9,10,11,12,13,14];
shuffle($cards);
$player1 = new ArrayIterator(array_slice($cards,0,7));
$player2 = new ArrayIterator(array_slice($cards,6,7));
$m = new MultipleIterator();
$m->attachIterator($player1);
$m->attachIterator($player2);
$pointsP1 = 0;
$pointsP2 = 0;
foreach($m as $currentCards) {
$text = 'player 1 has "%d" and player 2 "%d"';
echo sprintf($text, $currentCards[0], $currentCards[1]);
$number = 2;
if ($currentCards[0] > $currentCards[1]) {
$number = 1;
}
echo PHP_EOL .
sprintf('point goes to player %d', $number) .
PHP_EOL . PHP_EOL;
$points = 'pointsP' . $number;
$$points++;
}
$number = 2;
if ($pointsP1 > $pointsP2) {
$number = 1;
}
echo sprintf('player %d wins!', $number);
Result
player 1 has "9" and player 2 "3"
point goes to player 1
player 1 has "1" and player 2 "12"
point goes to player 2
player 1 has "2" and player 2 "13"
point goes to player 2
player 1 has "10" and player 2 "11"
point goes to player 2
player 1 has "5" and player 2 "8"
point goes to player 2
player 1 has "7" and player 2 "6"
point goes to player 1
player 1 has "3" and player 2 "14"
point goes to player 2
player 2 wins!