【问题标题】:using random numbers to execute a specific array function on an array使用随机数对数组执行特定的数组函数
【发布时间】: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)
  • sortrsort 也是如此
  • implode需要两个参数。

标签: php arrays sorting runtime


【解决方案1】:

对于数组输出,您必须使用 print_r 或 var_dump

还有

`$numArray = array(6, 98, 54, 13, 25, 70);

是一个错误,必须是

$numArray = array(6, 98, 54, 13, 25, 70);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    • 2017-02-02
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    相关资源
    最近更新 更多