我的功能是输出应有的功能。当我跑这个时,我得到的只是“那时众神伸出手来决定给予这个任务的力量。”如何让它从两个数组中选择相同的索引并填充echo中的变量?
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/>";
}
答案 0 :(得分: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",
));
答案 1 :(得分:0)
在$people
数组中,以$
开头的所有内容都被视为变量,因为您使用双引号("
)包围字符串。试试这个:
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/>";
}
答案 2 :(得分: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需要传递给函数或在函数中定义,除非它们已被删除,因此您可以为我们显示必要的代码。