0

我的功能是输出它应该输出的内容。当我运行这个程序时,我得到的只是“就在那时,众神伸出手,决定给予力量来帮助完成这项任务。” 如何让它从两个数组中选择相同的索引并填充回显中的变量?

function gift_giver()
{
    $people = array ("$heroname", "$friendname", "$wizardname", "Captain Rumbeard", "$frogname");
    $gifts = array("a magic compass", "the gift of no fear", "all seeing powers", "more rum", "a delightful lilly pad");
    $gift_selector=(rand(0,4));
    $gift_recipient=$people[$gift_selector];
    $gift_present=$gift[$gift_selector];
    echo "It was then that the gods reached out and decided to give $gift_recipient the power of $gift_present to aid in this quest.<br/><br/>";
}
4

3 回答 3

1

除了错误和未定义的变量,您的函数可能会从一些重构中受益:

function gift_giver(array $people, array $gifts)
{
    // take entry that will not overshoot either array
    $entry = rand(0, min(count($people), count($gifts)) - 1);

    printf(
        'It was then that the gods reached out and decided to give %s the power of %s to aid in this quest.<br/><br/>',
        $people[$entry],
        $gifts[$entry]
    );
}

gift_giver(['foo', 'bar'], ['baz', 'boo']);
// It was then that the gods reached out and decided to give bar the power of baz 
// to aid in this quest.<br/><br/>

这样,您的函数仅负责生成包含来自两个数组的输入的文本。为您的具体情况量身定制:

gift_giver(
    array($heroname, $friendname, $wizardname, "Captain Rumbeard", $frogname),
    array("a magic compass", "the gift of no fear", "all seeing powers", "more rum", "a delightful lilly pad")
);

更新

看到两个数组是如何相关的,您还可以考虑将它们映射到一个数组中:

function gift_giver(array $people_gift_map)
{
    $key = array_rand($people_gift_map);
    printf(
        'It was then that the gods reached out and decided to give %s the power of %s to aid in this quest.<br/><br/>',
        $key,
        $people_gift_map[$key]
    );
}

gift_giver(array(
    $heroname => "a magic compass", 
    $friendname => "the gift of no fear", 
    $wizardname => "all seeing powers", 
    "Captain Rumbeard" => "more rum", 
    $frogname => "a delightful lilly pad",
));
于 2013-09-13T04:45:02.517 回答
0

你失踪了$gift[$gift_selector]

function gift_giver($heroname, $friendname, $wizardname, $frogname){
    #or define $heroname $friendname $wizardname $frogname here
    $people = array($heroname, $friendname, $wizardname, "Captain Rumbeard", $frogname);
    $gifts = array("a magic compass", "the gift of no fear", "all seeing powers", "more rum", "a delightful lilly pad");
    $gift_selector = (rand(0,4));
    $gift_recipient = $people[$gift_selector];
    $gift_present = $gifts[$gift_selector];
    #                    ^missing s
    echo "It was then that the gods reached out and decided to give $gift_recipient the power of $gift_present to aid in this quest.<br/><br/>";
}

$heroname $friendname $wizardname $frogname 需要传递给函数或在函数中定义,除非它们已被删除,以便您可以为我们显示必要的代码。

于 2013-09-13T04:38:07.730 回答
0

在您的$people数组中,以 a 开头的所有内容都$被视为变量,因为您使用双引号 ( ") 将字符串括起来。尝试这个:

function gift_giver($heroname, $friendname, $wizardname, $frogname) {
    $people = array($heroname, $friendname, $wizardname, "Captain Rumbeard", $frogname);
    $gifts = array("a magic compass", "the gift of no fear", "all seeing powers", "more rum", "a delightful lilly pad");

    $gift_selector = rand(0,4);
    $gift_recipient = $people[$gift_selector];
    $gift_present = $gifts[$gift_selector];

    echo "It was then that the gods reached out and decided to give $gift_recipient the power of $gift_present to aid in this quest.<br/><br/>";
}
于 2013-09-13T04:35:59.573 回答