【发布时间】:2021-09-24 05:43:00
【问题描述】:
在运行时选择了一个 PHP 函数。从 1-5 生成一个随机数。对于每个随机数,应执行特定的数组函数,即 `shuffle()、sort()、rsort()、array_pop() 和 array_reverse()。
以下是我的代码,但它不正确。
`$numArray = array(6, 98, 54, 13, 25, 70);
$numbers = mt_rand(1, 5);
if($numbers == 1){
$next = array_pop($numArray);
echo $next;
}
elseif($numbers == 2){
$next = array_reverse($numArray);
echo implode($next);
}
elseif($numbers == 3){
$next = rsort($numArray);
echo ($next);
}
elseif($numbers == 4){
$next = sort($numArray);
echo ($next);
}
else{
$next = shuffle($numArray);
echo ($next);
}
【问题讨论】:
-
这有什么问题?不编译?不做该做的事?工作,但看起来不够好? ...或者?
-
对不起,好吧,所以我不认为它给了我正确的结果......对于一个 shuffle 方法返回 1 作为结果
-
shuffle作用于数组本身,只返回真或假 (manual) -
sort和rsort也是如此 -
而
implode需要两个参数。
标签: php arrays sorting runtime