Rand()选择数组元素


rand() to slelect array elements

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

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

当你有了你的获奖名字,用大写字母打印到屏幕上,这样每个人都知道获胜者有多棒。

我想在这里使用的函数: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>

既然您需要使用特定的函数,这里就是答案。

<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>