Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
Finding lottery winners

Pretty sure my syntax is off, been a while since I've used PHP - any help is appreciated! Kind of a mix match of C at the moment. This is essentially a loop that's going through comparing both arrays before being passed to a second loop that throws in the rule that is required to win the 'lottery'

$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]++;
        }
    }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I don't intend for up/downvote.

I recommend You to learn PHP essentials (YouTube: PHP tutorials for beginner)

About checking lottery winners - it can be done just intersecting 2 arrays and checking intersection size:

$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);
over 4 years ago · Santiago Trujillo Relatório

0

This might be what you are after

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

RESULTS

Jack is a winner
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda