随机化变量时未定义的偏移量?

时间:2018-01-06 18:35:15

标签: php random error-handling

$units = array(
    "R" =>"Kid Pan", "PO Krillen", "Android #16", "Android #19",
    'SR'=>'Vegeta', 'Piccolo', 'Botamo', 'SSJ2GokuGT', 'CaptainGinyu',
    'SSR'=>'TEQPerfectCell', 'STRGogeta', 'PHYKidBuu', 'STRBroly',
);
    echo $units[rand(0, count($units) - 1)] . "\n";

此代码工作正常,但有时会出现错误,有些则有不同的数字? 例如; "注意未定义的偏移量:第8行和第34行的第10行; 注意未定义的偏移量:第8行的第12行

顺便说一句,我使用 http://phptester.net/

测试此代码

(我是一个菜鸟 - 对不起:/)

2 个答案:

答案 0 :(得分:0)

尝试使用array_rand

echo $units[ array_rand($units)] . "\n";

http://php.net/manual/en/function.array-rand.php

答案 1 :(得分:0)

您无法回显阵列 您已使用print_r()来查看数组的值

print_r( $units[ array_rand($units) ] ) . "\n";
相关问题