I have a code to roll a rand number, and show the winner based on their chance.
$data = array();
foreach($getAllUserTicketHistoryJson as $value){
for($i=0;$i<2;$i++)
{
chance($getAllUserTicketHistoryJson[rand(0,count($getAllUserTicketHistoryJson))]);
}
function chance($arrWinner = array()){
// the array for winner is
echo "<pre> Winner array ";
print_r($arrWinner);
echo "</pre>";
}
try this... and if there is an errer... then let me know ... or add screen short of error if possible
<?php
$ticket_numbers = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);
shuffle($ticket_numbers);
$number_winners_required_per_round = 5;
$winner_count = 0;
$number_of_rounds = 3;
$round_number = 0;
while ( $round_number < $number_of_rounds) {
$round_number++;
echo "Round $round_number:";
while ($winner_count < $number_winners_required_per_round ) {
$winner[$winner_count] = array_pop($ticket_numbers);
echo "<p>The winning ticket number $winner_count is ticket number: {$winner[$winner_count]}</p>";
$winners[$round_number][] = $winner[$winner_count];
$winner_count++;
}
$winner_count = 0;
}
var_dump($winners);
?>
$ticket_numbers will be filled from the JSON feed. Each time the main loop is run, the required number of winners are picked, their ticket numbers are removed from the list of potential winners. An array of winners is generated which will be used for entering the winners into the database. Each ticket can only win once.
Change your code with
for($i=0;$i<2;$i++)
{
chance($getAllUserTicketHistoryJson[rand(0,count($getAllUserTicketHistoryJson))]).'<br><br>';
}
function chance($arrWinner = array()){
// the array for winner is
echo "<pre> Winner array : ";
print_r($arrWinner);
echo "</pre>";
}
I'll not be exactly accurate with what you have already asked but give you an alternative approach how to solve the problem. Lets restate the chance first of all as a single entity and rename it ticket. Each ticket has unique and equal chance to be selected in each round.
Lets say you have sold a total of 100 tickets in lottery X. The given lottery is consisted of 5 rounds
$rounds = 1; // starting round
$maxRounds = 5;
$tickets = range( 1, 100 );
If you need to compare the chances that a player has, holding on a number of tickets it is a different data-set that you should retrieve from your database. That would require an additional table holding this data but it should not "cook" the chance of a ticket to appear. If you need that stat here is an example (in php)
// total of 100 players
$players = range( 1, 100 );
$players = array_flip( $players );
// assign a random number of purchases per player
for( $i = 1; $i <= 100; $players[$i] = mt_rand( 1, 20 ), ++$i );
That would result to a total of
$tickets = range( 1, array_sum( $players ) );
Back to the original problem. You now have a total of sold tickets
, and each lottery has 5 rounds. Thus,
$rounds = 1;
$maxRounds = 5;
while( $rounds <= $maxRounds )
{
$winners = array_rand( $tickets, 3 );
$tickets = array_diff( $tickets, $winners );
shuffle( $winners );
foreach( $winners as $pos => $winner )
{
++$pos;
echo "Loterry: {$rounds} - Price {$pos} - winner is {$winner}<br />";
}
++$rounds;
}
Time to tweak the echo with something you will be able to store in your db in a single go. So I'll rewrite the last part for clarity. NOTE since this is a simple example and is data you control you can add this to your database without filtering it.
$rounds = 1;
$maxRounds = 5;
$insert = [ ];
while( $rounds <= $maxRounds )
{
$winners = array_rand( $tickets, 3 );
$tickets = array_diff( $tickets, $winners );
shuffle( $winners );
$insert[$rounds][ ] = $rounds;
foreach( $winners as $winner )
{
$insert[$rounds][ ] = $winner;
}
$insert[$rounds] = '(' . implode( ',', $insert[$rounds] ) . ')';
++$rounds;
}
$sql = sprintf( "INSERT INTO `lotteries`( `round`, `first`, `second`, `third` ) VALUES %s;", implode( ', ', $insert ) );
From here on you can use this statement to insert at your db. Just change the table name and the fields to what you actually have in your database. I hope it helped