PHP警告:array_rand()

时间:2018-11-02 17:48:45

标签: php

$d = 'Neo , Morpheus , Trinity , Cypher , Tank';
$input = array($d);

$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
  

PHP警告:array_rand():第二个参数必须在1到第4行的/home/blogsvie/public_html/a.php中数组元素的数量之间

1 个答案:

答案 0 :(得分:0)

您的数组只有一个元素。包含“ Neo,Morpheus,Trinity,Cypher,Tank”的字符串;

创建一个包含多个元素的数组,或将该字符串分解为数组:

$d = 'Neo,Morpheus,Trinity,Cypher,Tank';
$input = explode(",",$d);
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";