rand()选择数组元素

时间:2014-05-29 12:21:04

标签: php arrays sorting join random

我想创建一个数组(),其中包含您最亲密的家人和朋友的名字。

在我创建了我的列表后,我需要对其进行排序并从列表中随机选择一个名称。

如果您有获奖名称,请将其打印到屏幕上,这样每个人都知道获胜者有多棒。

我想在这里使用函数:array(),array_push(),sort(),count(),rand()和strtoupper()。 现在代码:

<html>
    <p>
    <?php
    // Create an array and push on the names
    // of your closest family and friends
    $family = array("jan","klaas","oma");
    // Sort the list
    sort($family);
    join(", ", $family);
    // Randomly select a winner!
   $family rand(0, 2);
    // Print the winner's name in ALL CAPS
    ?>
    </p>
</html>

1 个答案:

答案 0 :(得分:0)

因为你需要使用特定的功能才是答案。

<html>
    <p>
    <?php
    // Create an array and push on the names
    // of your closest family and friends
    $family = array();
    array_push($family,"jan");
    array_push($family,"klaas");
    array_push($family,"oma");

    // Sort the list
    sort($family);
    $count = count($family);

    // Randomly select a winner!
    $selected = rand(0,$count-1);


    // Print the winner's name in ALL CAPS
    echo strtoupper($family[$selected]);
    ?>
    </p>
</html>