Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda