首页 > 解决方案 > 如何在 PHP 中使用用户输入从多个数组中随机生成对象?

问题描述

我正在尝试创建一个生成器,用户可以在其中输入要生成的项目数量。每次都会生成一个从 1 到 100 的数字。根据结果​​,从三个数组之一中随机选择一个项目。它应该根据需要循环,然后显示结果。当我尝试遵循逻辑时,我在下面有部分代码。

<form action='random.php' method='POST' id='random'>
   <table class='table table-responsive'>
     <tr>
        <td>Number of items: </td>
        <td><input type='text' name='randitem'></td>
        <td><button type="submit"Generate</button>
        </td>
     </tr>
   </table>
<?php

$roll = rand(1, 100);

    if $roll < 50

        $commonItem = array(citem1, citem2, citem3);

    if $roll => 50 and < 95

        $uncommonItem = array(uitem1, uitem2, uitem3);

    if $roll => 95

        $rareItem = array(ritem1, ritem2, ritem3);
?>

标签: phphtml

解决方案


这是一个想法,您可以根据要选择的数组从每个数组中随机选择一个项目(根据您的示例代码也是随机的):

<?php
    $commonItems = array('citem1', 'citem2', 'citem3');
    $uncommonItems = array('uitem1', 'uitem2', 'uitem3');
    $rareItems = array('ritem1', 'ritem2', 'ritem3');
    $roll = rand(1,100);

    if ($roll < 50) {

         $size = count($commonItems) - 1;
         $index = rand(0,$size); 
         $item = $commonItems[$index];

    } elseif ($roll >= 50 && $roll < 95) {

         $size = count($uncommonItems) - 1;
         $index = rand(0,$size); 
         $item = $uncommonItems[$index];

    } else {

         $size = count($rareItems) - 1;
         $index = rand(0,$size); 
         $item = $rareItems[$index];

    }

    echo $item;
?>

这是一个重构版本,其中包含一个处理 if 语句中重复功能的函数:

<?php
    $commonItems = array('citem1', 'citem2', 'citem3');
    $uncommonItems = array('uitem1', 'uitem2', 'uitem3');
    $rareItems = array('ritem1', 'ritem2', 'ritem3');

    function getItem($itemArray) {    
         $size = count($itemArray) - 1;
         $index = rand(0,$size); 
         return $itemArray[$index];    
    }

    $roll = rand(1,100);
    if ($roll < 50) {
        $item = getItem[$commonItems];
    } elseif ($roll >= 50 && $roll < 95) {
        $item = getItem[$uncommonItems];
    } else {
        $item = getItem[$rareItems];
    }

    echo $item;
?>

编辑:我错过了关于如何输入多个项目的部分,所以这就是

    // Check to see if form was submitted with something in randitem
    if (isset($_POST['randitem'])) {

        $commonItems = array('citem1', 'citem2', 'citem3');
        $uncommonItems = array('uitem1', 'uitem2', 'uitem3');
        $rareItems = array('ritem1', 'ritem2', 'ritem3');

        function getItem($itemArray) {    
            $size = count($itemArray) - 1;
            $index = rand(0,$size); 
            return $itemArray[$index];    
        }

        // loop number of times set in form
        foreach ($i = 0; $i < $_POST['randitem']; $i++) {

            $roll = rand(1,100);
            if ($roll < 50) {
                $item = getItem[$commonItems];
            } elseif ($roll >= 50 && $roll < 95) {
                $item = getItem[$uncommonItems];
            } else {
                $item = getItem[$rareItems];
            }

            echo $item . "<br>"; // will print each item on its own line
        }
    }
?>

注意:如果提交的表单没有数字,这将不起作用,因此请考虑将输入类型更改为“数字”或对表单进行一些其他验证,以确保仅向 PHP 发送数字


推荐阅读