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

配列では、文字列を二重引用符 ( )で囲んでいるため、 $peoplea で始まるすべて$が変数として扱われます。"これを試して:

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 に答える