php - 随机变量时 undefined offset ?

标签 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";

这段代码可以正常工作,但有时会出错,有些带有不同的网络号?
例如; “NOTICE undefined offset :第8行上为10” 注意 undefined offset :第8行上为

顺便说一句,我正在使用http://phptester.net/测试此代码

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

最佳答案

尝试使用array_rand

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

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

关于php - 随机变量时 undefined offset ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48130573/

相关文章:

PHP - 对多维数组进行排序 - 问题

java - 如何在我的程序中为随机数分配一个字符串值

php - 如何处理PHP 5.4中的方法引发的异常?

c++ - 将 srand 用于随机种子时重复输出

delphi - Delphi 有加密安全的 PRNG 库吗?

python - flask : Ignore if one file is not uploaded in form (containing multiple file upload options)

ios - iOS二进制文件由于启动时崩溃而被拒绝,无法重新创建,只有错误日志可以从

php - 文本加权到段落的最后一行

php - 如何在 PHP/MySQL API 的结果集中查找字段的源数据库名称?

javascript - 循环到 php 数组的每一行