【问题标题】:stuck here with lotto grid卡在这里与乐透网格
【发布时间】:2011-03-15 12:16:45
【问题描述】:

我有一个有 42 个 nrs 的网格,我将使用 rand() 函数从网格中挑选出数字并标记它们

到目前为止,我想出了

    <?php
    $row="";
    print ("<table border=\"1\">");
    for ($i=0; $i<6; $i++)
        {
        print ("<tr>");
            for ($j=0; $j<7; $j++)
                {
                $random = rand(1,42);
                $row += "(string)$random";
                $som = $som + 1;
                print("<th>".$som);
                }

        ("</tr>");
        }

    print ("</table>");
    print ("$rij");

 // here I'm just testing to see if I can get a list of random numbers 
 for ($i=0; $i<6; $i++){
        $randomNr = rand(1,42);
        echo "$randomNr<br/>";
        }


    ?>

我想这个想法是将 rand 函数中的数字与表的索引相匹配。但我真的被困在这里让表格转换为一个arra,所以我可以将索引与随机数匹配。

【问题讨论】:

    标签: php arrays function


    【解决方案1】:

    您自己的尝试可能不会太远。您只需要生成 6 个随机的 unique 数字并与它们进行比较。最简单的方法是使用range() 生成一个数组并使用array_rand() 选择随机数(它实际上返回索引,因此您需要一些额外的代码来获取值)。然后你只需要使用in_array()查找当前输出的数字是否在选择的数字数组中

    这是一个一般情况的示例函数,它稍微扩展了 Sondre 的示例。示例中的函数采用以下参数:选取的随机数总数、网格中的最小数字、网格中的最大数字以及网格中每行的数字。该函数将生成的 HTML 表源返回一个字符串。

    <?php
    
    function generateHighlightedLotteryTable ($count = 6, $min = 1, $max = 42, $perRow = 7)
    {
        // Generate the picked numbers (actually we just get their indexes)
        $nums = array_rand(range($min, $max), $count);
    
        $output = "<table>\n";
    
        for ($n = $min; $n <= $max; $n++)
        {
            // get "index" of the number, i.e. $min is the first number and thus 0
            $i = $n - $min;
    
            if ($i % $perRow == 0)
            {
                $output .= "<tr>";
            }
    
            // If the current number is picked
            if (in_array($i, $nums))
            {
                $output .= "<td><strong>$n</strong></td>";
            }
            // If the current number hasn't been chosen
            else
            {
                $output .= "<td>$n</td>";
            }
    
            if ($i % $perRow == $perRow - 1)
            {
                $output .= "</tr>\n";
            }
        }
    
        // End row, if the numbers don't divide evenly among rows
        if (($n - $min) % $perRow != 0)
        {
            $output .= "</tr>\n";
        }
    
        $output .= "</table>";
    
        return $output;
    }
    
    echo generateHighlightedLotteryTable();
    
    ?>
    

    我希望这就是你想要实现的目标。

    【讨论】:

    • 是的,这是一个很好的结果,这就是我正在寻找的。太棒了,谢谢!
    【解决方案2】:

    这将创建一个由 42 个数字组成的网格并标记出一个随机数字。如果您想标记更多的创建和数组,并检查仅包含 rand 变量的内容。在您的原始代码中,您实际上运行了 rand-function 42 次,我猜这是无意的。

    编辑:或者您是否需要用随机数填充网格?

    $rand = rand(1, 42);
    
    echo "<table>";
    for($i = 1;$i <= 42; $i++) {
        if($i%7 == 1) {
            echo "<tr>";
        }
        $print = $rand == $i ? "<strong>" . $i . "</strong>" : $i;
        echo "<td>" . $print . "</td>";
        if($i%7 == 0) {
            echo "</tr>";
        }
    }
    echo "</table>";
    

    【讨论】:

    • 不,我不需要填充随机数的网格。我正在制作一个数组并用随机数填充它。 PHP 对我来说很新,但我的猜测已经是我的问题所在。基本上我需要6个不同的随机数,需要匹配网格中对应的数字并点亮。
    猜你喜欢
    • 2012-10-04
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多