如何修复未定义的偏移量和未定义的索引


How to fix undefined offset and undefined index

你好,我有一个代码,它从20个项目的列表中选择12个,从这些项目中随机选择1个并返回:

    class Gamble
    {
        public static function doPointsCheck()
        {
            global $Gamble;
            $Gamble_points = '2';
        if($Gamble_points <= 1)
                return false;
            else
                return true;
        }
        public static function Spin()
        {
            global $Wheel;
            if(!self::doPointsCheck())
            return 'You need way too more points to gamble';
            else
            {
                $Result = array();
                    $Result = range(1, 12);
                    shuffle($Result);
                    $Result = array_slice($Result, 0, 20);
                $SendToCheck = self::CheckPrize($Result);
            }
        }
        public static function CheckPrize($Array)
        {
            global $Wheel;
            echo '<h1>List of items you can win today:</h1><form action="class.MysticWheel.php" method="post">';
            for($i = 1; $i <= count($Array); $i++)
            {
                $Won = '<p>'.$Wheel[$Array[$i]]['pseudo'].'</p>';
                echo $Won;
            }
            echo '<input name="Feeling lucky" type="submit" value="Feeling Lucky" /></form>';
            if ($_SERVER['REQUEST_METHOD'] === 'POST'){
                $i = rand( 1, 12 );
                $Prize = '<p>'.$Wheel[$Array[$i]]['pseudo'].'</p>';
                echo $Prize;
            }
        }
    }

当我测试它时,我收到以下错误:

注意:未定义偏移:第54行12

注意:未定义的索引:在第54行

所以我不会显示最后一个项目,它只会显示11个可能的项目,这些项目本可以赢得并收到通知。

我该怎么修?

$Result = range(1, 12);
-->
foreach (range(1, 12) as $number) {
    $Result[] = $number;
}

试试这个。