非常确定我的语法已关闭,因为我使用过PHP已经有一段时间 - 任何帮助都表示赞赏!目前C的混合比赛。这本质上是一个循环,在传递给第二个循环之前比较两个数组,这个循环抛出了赢得“彩票”所需的规则
$pickedBalls = [1, 2, 3, 4, 5, 6];
$playerBalls = [
"Harry" => [6, 7, 8, 9, 10, 11],
"Jack" => [4, 5, 6, 7, 8, 9],
"Andrew" => [9, 10, 11, 12, 13, 14],
"Paul" => [15, 16, 17, 18, 19, 20],
"Andrew2" => [21, 22, 23, 24, 25, 26]
];
$results = [];
foreach ($playerBalls as $key => $value) {
for ($i = 0; $i < 6; $i++) {
for ($j = 0; $j < 6; $j++) {
if ($pickedBalls[$i] == $value[$j])
$results[$key]++;
}
}
}
答案 0 :(得分:1)
我不打算上/下投票。
我建议您学习PHP基础知识(YouTube:初学者的PHP教程)
关于检查彩票中奖者 - 只需交叉2个数组并检查交叉点大小即可:
$drawnNumbers = [4, 8, 15, 16, 23, 42];
$ticketsPurchases = [
"Harry" => [3, 2, 31, 5, 12, 44],
"Jack" => [8, 42, 13, 23, 1, 49],
"Andrew" => [8, 17, 19, 22, 25, 31],
"Paul" => [11, 16, 20, 29, 31, 38],
"Andrew" => [17, 18, 20, 22, 31, 47]
];
$results = [];
$winners = [];
$winningCondition = 3;
foreach ($ticketPurchases as $player => $numbers)
{
$winningNumbers = array_intersect($numbers, $drawnNumbers); // intersection of player's ticket numbers and drawn numbers
$winningNumbersCount = sizeof($winningNumbers); // intersection size
$won = $winningNumbersCount >= $winningCondition; // if intersection size GTE winning condition (3)
$results[$player] = compact('player', 'winningNumbers', 'winningNumbersCount', 'won');
if($won) $winners[] = $player;
}
echo "\nRESULTS:\n";
print_r($results);
echo "\nWINNERS:\n";
echo implode("\n", $winners);
答案 1 :(得分:0)
这可能就是你的目标
$pickedBalls = [4, 8, 15, 16, 23, 42];
$playerBalls = [
"Harry" => [3, 2, 31, 5, 12, 44],
"Jack" => [8, 42, 13, 23, 1, 49],
"Andrew" => [8, 17, 19, 22, 25, 31],
"Paul" => [11, 16, 20, 29, 31, 38],
"Andrew2" => [17, 18, 20, 22, 31, 47]
];
$results = [];
foreach ($playerBalls as $person => $balls) {
if ( !isset($results[$person]) ) {
$results[$person] = 0;
}
foreach( $balls as $ball) {
if (in_array($ball, $pickedBalls)) {
$results[$person]++;
}
}
}
foreach ( $results as $name => $balls ) {
if ( $balls >= 3) {
echo $name . ' is a winner';
}
}
结果
Jack is a winner