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

时间:2014-04-15 18:58:46

标签: php

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

    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个可能已赢得的项目并获得通知。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

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

试试这个。