PHP回送随机数组并插入数据库,无需重复

PHP回送随机数组并插入数据库,无需重复,php,arrays,database,Php,Arrays,Database,我有一个代码来滚动一个兰特号码,并根据他们的机会显示胜利者 $data = array(); foreach($getAllUserTicketHistoryJson as $value){ $data[$value['user_id']] = number_format((float)($value['total_ticket'] / $getAllTicketRound * 100), 2, '.', ''); } $array=$data;

我有一个代码来滚动一个兰特号码,并根据他们的机会显示胜利者

    $data = array();
    foreach($getAllUserTicketHistoryJson as $value){
        $data[$value['user_id']] = number_format((float)($value['total_ticket'] / $getAllTicketRound * 100), 2, '.', '');
    }
    $array=$data;
function chance($input=array())
{
    $number=rand(0,array_sum($input));
    $starter=0;
    foreach($input as $key => $val)
    {
        $starter+=$val;
        if($number<=$starter)
        {
                $ret=$key;
                break;
        }

    }

    return 'Winner is '.$ret.'<br/>';
    }
for($i=0;$i<3;$i++)
{
    echo chance($array).'<br><br>';
}
问题是,用户“4”在第一轮和最后一轮中两次获胜。如何防止赢家赢两次

以及如何将每个获胜者插入数据库

我的数据库是这样的

=========================================
id  |  Round  |  first |  Second  | third
=========================================
1   |    1    |   4    |    3     |   1
=========================================
我想将每个获胜者插入数据库“第一”、“第二”和“第三”。因此,赢家ID不会加倍以防止重复

请帮助。

使用更改代码

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>";
}
for($i=0;$i
for($i=0;$i


$ticket_编号将从JSON提要中填充。每次运行主循环时,都会选择所需数量的中奖者,他们的票号将从潜在中奖者列表中删除。将生成一个中奖者数组,用于将中奖者输入数据库。每张票只能赢一次。

你已经提出的要求是准确的,但给你一个解决问题的替代方法。让我们首先将机会作为一个单独的实体重申,并将其重命名为ticket。在每一轮中,每张ticket都有唯一且平等的机会被选中

假设您在彩票X中总共售出了100张彩票。给定的彩票由5轮组成

$tickets = range( 1, array_sum( $players ) );
如果您需要比较一个玩家持有多张罚单的机会,那么您应该从数据库中检索不同的数据集。这需要一个额外的表来保存这些数据,但它不应该“烹饪”罚单出现的机会。如果您需要该统计数据,这里是一个示例(在php中)

回到原来的问题。您现在总共售出了
彩票,并且每个彩票有5轮。因此

$rounds=1;
$maxRounds=5;
while($rounds$winner)
{
++$pos;
echo“Loterry:{$rounds}-Price{$pos}-winner是{$winner}
”; } ++$rounds; }
是时候用一些你可以一次性存储在数据库中的东西来调整回音了。为了清晰起见,我将重写最后一部分。注意因为这是一个简单的示例,是你可以控制的数据,你可以将其添加到数据库中,而无需对其进行过滤

$rounds=1;
$maxRounds=5;
$insert=[];

虽然($rounds您应该看一看,一旦一张票子赢了,它们是否意味着从接下来的几轮中被淘汰?是的,例如,如果用户4已经在第一轮中获胜,那么用户4就不能再赢了。在哪里可以得到$arrWinner字符串?我的是$inputry,即编辑后的一个字符串…在$arrWinner中,您将得到赢家的数组…使用该代码,您将仅获取3个数组…使用它…让fungot处理此错误注意:未定义的偏移量:4个在前赢家数组中:注意:未定义的偏移量:4个在C:\xampp\htdocs\…在第75行赢家数组中,第75行是chance($GetAllUserTicketThistoryJSON[rand(0,count($GetAllUserTicketThistoryJSON)));很好的分享,你能在那里添加一个机会吗?因为我的代码使用用户id和他们的机会,所以$array=array这样的$data输出('BLICK'=>15.30,'brown'=>20.20,'kitty'=>15.30,'lala'=>15.20,'popi'=>14.00,'usher'=>20.00);
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>";
}
<?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);
?>
$rounds = 1; // starting round
$maxRounds  = 5;
$tickets = range( 1, 100 );
// 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 );
$tickets = range( 1, array_sum( $players ) );
$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;
}
$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 ) );