PHP Echo Random Array and Insert to Database without repeat

后端 未结 4 704
滥情空心
滥情空心 2021-01-29 07:09

I have a code to roll a rand number, and show the winner based on their chance.

    $data = array();
    foreach($getAllUserTicketHistoryJson as $value){
                


        
4条回答
  •  后悔当初
    2021-01-29 08:07

    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}
    "; } ++$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

提交回复
热议问题