配列から読み込む関数をPHPで作成しようとしています。配列の値を読み込むために、テキストと配列のID番号を連結しようとしていますが、配列の値ではなく連結されたテキストが返されます。ここでphpの配列項目にアクセスするためにテキスト値を連結する
は私のコードです:
//arrays with words in dictionary
$dictionary_word1 = array("test1","test2","test3");
$dictionary_word2 = array("test4","test5","test6");
$dictionary_word3 = array("test7","test8","test9");
$word_to_lookup = "dictionary_word_1";
//value to send to function
$returned_word = convert_word($word_to_lookup);
//value returned from function
echo "<br>the returned word from the function is " . $returned_word;
//the text "$dictionary_word_1[2]" is displayed instead of array value
echo "<br>the value in the array is " . $dictionary_word1[2];
//this displays correcty as "test3"
function convert_word($word_to_convert)
{
global $dictionary_word1;
$converted_word = '$'.$word_to_convert .'[2]';
return $converted_word;
}
は、誰も私に私が間違っているつもりどこに任意のヒントを与えてもらえますか?あなたは(PHP: Variable VariablesとPHP: Variable Parsingを参照)変数にあなたがしようとしている何をすべきか、このように参照する必要
$ converted_word = $ {$ word_to_convert} [2];しかし、あなたがやっていることをやるより良い方法があります。 – AbraCadaver