【问题标题】:How to randomly pull same index from two arrays?如何从两个数组中随机提取相同的索引?
【发布时间】:2013-09-13 04:31:00
【问题描述】:

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

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/>";
}

【问题讨论】:

  • 一方面,它应该是$gifts,而不是$gift
  • 我在编辑中修复了这个问题;等待批准....
  • @GeorgeBrighton 你不应该在编辑中修复错误。
  • $heroname 在哪里定义?在你的职能之外?

标签: php function variables random


【解决方案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",
));

【讨论】:

  • OP 希望礼物/人的数组索引相同,而不是随机的。
  • @HenryEdwardQuinnIV 已更新。
  • @Class 确实如此,但在这种情况下,OP 还不如使用单个数组,映射每个人 => 礼物 :)
  • @Jack 这将使 OP 的事情变得更简单。
【解决方案2】:

在您的$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/>";
}

【讨论】:

  • 好吧,它们是由用户设置的,因为这是为了讲述一种疯狂的图书馆类型的故事。知道如何解决这个问题吗?
  • 我用单引号做了这件事,得到了“...决定给 $friendname 的权力来帮助完成这个任务。”
  • 它会像 PHP 现在将它们视为文字字符串一样。我现在已将它们转换为参数 - 可能是您最初想要的 :)
【解决方案3】:

你在$gift[$gift_selector]中缺少一个s

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 需要传递给函数或在函数中定义,除非它们已被删除,以便您可以为我们显示必要的代码。

【讨论】:

  • 刚刚按照您的方式尝试,它修复了显示的礼物,但即使将变量传递给函数,名称也不会出现在 echo 中。
  • @HenryEdwardQuinnIV 你是怎么调用函数的?
猜你喜欢
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多