用于抽奖的PHP数组

ilmyapht  于 2022-12-25  发布在  PHP
关注(0)|答案(1)|浏览(113)
    • 我在尝试打造一个raffle系统**
  • 可能的总数为60,000
  • 我卖了一万张票
  • 我必须使用50,000张未使用的彩票......因此每张彩票都有额外的"机会"赢取。50,000张未售出彩票除以10,000张售出彩票
  • 每张门票可获得5次额外机会。

知道了这一点,我需要的是这样的东西:

    • 票证:机会1、机会2、机会3、机会4、机会5**
00001: ["20000","20001","40000","40001","60000"] 
00002: ["19999","20002","39999","40002","59999"]
00003: ["19998","20003","39998","40003","59998"] 
00004: ["19997","20004","39997","40004","59997"] 
00005: ["19996","20005","39996","40005","59996"]
.
.
.
09995: ["10006","29995","30006","49995","50006"]
09996: ["10005","29996","30005","49996","50005"]
09997: ["10004","29997","30004","49997","50004"]
09998: ["10003","29998","30003","49998","50003"]
09999: ["10002","29999","30002","49999","50002"]
10000: ["10001","30000","30001","50000","50001"]

因为我不能用已经卖出的一万张票。
第一列从20,000开始,下降到10,001;第二列从20,001开始,结束于30,000,依此类推。
我已经有了这个代码,它是这样工作的。

function randomGen($min, $max, $quantity) {
        $numbers = range($min, $max);
        $find = array("[","]");
        $replace = array("","");
        $cadena = str_replace($find, $replace, json_encode(array_slice($numbers, 0, $quantity)));
        return $cadena;
    }

    $minCol1 = 20000;
    $maxCol1 = 10001;

    $minCol2 = 20001;
    $maxCol2 = 30000;

    $minCol3 = 40000;
    $maxCol3 = 30001;

    $minCol4 = 40001;
    $maxCol4 = 50000;

    $minCol5 = 60000;
    $maxCol5 = 50001;
    for($i = 1; $i<=10000; $i++){
        $oportunidades = randomGen($minCol1,$maxCol1,1).",".randomGen($minCol2,$maxCol2,1).",".randomGen($minCol3,$maxCol3,1).",".randomGen($minCol4,$maxCol4,1).",".randomGen($minCol5,$maxCol5,1);
        $str_arr = explode (",", $oportunidades);
        ?>
        <li>
            <span data-oportunidades='<? echo(json_encode($str_arr));?>'>
                <?=str_pad($i, 5, "0", STR_PAD_LEFT);?>
            </span>
        </li>
    <?
        $minCol1 = $minCol1 -1;
        $maxCol1 = $minCol1 -1;

        $minCol2 = $minCol2 +1;
        $maxCol2 = $minCol2 +1;

        $minCol3 = $minCol3 -1;
        $maxCol3 = $minCol3 -1;

        $minCol4 = $minCol4 +1;
        $maxCol4 = $minCol4 +1;

        $minCol5 = $minCol5 -1;
        $maxCol5 = $minCol5 -1;
    }

但我很肯定这不是解决这个问题的正确方法。
有人知道怎么做吗?
太感谢你了!

0pizxfdo

0pizxfdo1#

也许你想要这样的东西:

// This is an example function to generate initial tickets
function genTickets(int $num): array
{
    $tickets = [];

    for ($i = 0; $i < $num; $i++) {
        $tickets[] = $i + 1;
    }

    return $tickets;
}

function getTicketsSlice(array $tickets, bool $ascending, int $count, int $offset): array
{
    $slice = array_slice($tickets, $offset, $count);

    if (!$ascending) {
        return array_reverse($slice);
    }

    return $slice;
}

function getSoldTicketsWithOpportunities(array $tickets, int $soldTicketsCnt): array
{
    $opportunities = (int)floor((count($tickets) - $soldTicketsCnt) / $soldTicketsCnt);

    $soldTickets = [];

    for ($i = 0; $i < $opportunities; $i++) {
        $ascending = $i % 2 !== 0;

        foreach (getTicketsSlice($tickets, $ascending, $soldTicketsCnt, ($i + 1) * $soldTicketsCnt) as $idx => $sliceItem) {
            $soldTickets[$idx][] = $sliceItem;
        }
    }

    return $soldTickets;
}

$totalTicketsCnt = 60000;
$soldTicketsCnt = 10000;

$tickets = genTickets($totalTicketsCnt);
$soldTickets = getSoldTicketsWithOpportunities($tickets, $soldTicketsCnt);

foreach ($soldTickets as $ticketId => $opportunities) {
    ?>
    <li>
        <span data-oportunidades='<?= json_encode($opportunities, JSON_THROW_ON_ERROR) ?>'>
            <?= str_pad((string)($ticketId + 1), 5, "0", STR_PAD_LEFT) ?>
        </span>
    </li>
    <?php
}

或者以不那么自然而然的方式:

// This is an example function to generate initial tickets
function genTickets(int $num): array
{
    $tickets = [];

    for ($i = 0; $i < $num; $i++) {
        $tickets[] = $i + 1;
    }

    return $tickets;
}

function getTicketsSlice(array $tickets, bool $ascending, int $count, int $offset): array
{
    $slice = array_slice($tickets, $offset, $count);

    if (!$ascending) {
        return array_reverse($slice);
    }

    return $slice;
}

$totalTicketsCnt = 60000;
$soldTicketsCnt = 10000;

$tickets = genTickets($totalTicketsCnt);

// Get tickets slice from 10000 to 20000 in reverse order
foreach (getTicketsSlice($tickets, false, $soldTicketsCnt, 10000) as $sliceItem) {
    $soldTickets[] = [$sliceItem];
}

// Apply next opportunities in ascending order
foreach (getTicketsSlice($tickets, true, $soldTicketsCnt, 20000) as $idx => $sliceItem) {
    $soldTickets[$idx][] = $sliceItem;
}

// Apply next opportunities in descending order
foreach (getTicketsSlice($tickets, false, $soldTicketsCnt, 30000) as $idx => $sliceItem) {
    $soldTickets[$idx][] = $sliceItem;
}

// Apply next opportunities in ascending order
foreach (getTicketsSlice($tickets, true, $soldTicketsCnt, 40000) as $idx => $sliceItem) {
    $soldTickets[$idx][] = $sliceItem;
}

// Apply next opportunities in descending order
foreach (getTicketsSlice($tickets, false, $soldTicketsCnt, 50000) as $idx => $sliceItem) {
    $soldTickets[$idx][] = $sliceItem;
}

foreach ($soldTickets as $ticketId => $opportunities) {
    ?>
    <li>
        <span data-oportunidades='<?= json_encode($opportunities, JSON_THROW_ON_ERROR) ?>'>
            <?= str_pad((string)($ticketId + 1), 5, "0", STR_PAD_LEFT) ?>
        </span>
    </li>
    <?php
}

相关问题