Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!